home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / prim / simple.el.z / simple.el
Encoding:
Text File  |  1998-05-21  |  141.2 KB  |  3,867 lines

  1. ;;; simple.el --- basic editing commands for XEmacs
  2.  
  3. ;; Copyright (C) 1985, 1986, 1987, 1993-1997 Free Software Foundation, Inc.
  4. ;; Copyright (C) 1995 Tinker Systems and INS Engineering Corp.
  5.  
  6. ;; This file is part of XEmacs.
  7.  
  8. ;; XEmacs is free software; you can redistribute it and/or modify it
  9. ;; under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 2, or (at your option)
  11. ;; any later version.
  12.  
  13. ;; XEmacs is distributed in the hope that it will be useful, but
  14. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. ;; General Public License for more details.
  17.  
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  20. ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  21. ;; 02111-1307, USA.
  22.  
  23. ;;; Synched up with: FSF 19.34 [But not very closely].
  24.  
  25. ;;; Commentary:
  26.  
  27. ;; A grab-bag of basic XEmacs commands not specifically related to some
  28. ;; major mode or to file-handling.
  29.  
  30. ;; Changes for zmacs-style active-regions:
  31. ;;
  32. ;; beginning-of-buffer, end-of-buffer, count-lines-region, 
  33. ;; count-lines-buffer, what-line, what-cursor-position, set-goal-column,
  34. ;; set-fill-column, prefix-arg-internal, and line-move (which is used by
  35. ;; next-line and previous-line) set zmacs-region-stays to t, so that they
  36. ;; don't affect the current region-hilighting state.
  37. ;;
  38. ;; mark-whole-buffer, mark-word, exchange-point-and-mark, and
  39. ;; set-mark-command (without an argument) call zmacs-activate-region.
  40. ;;
  41. ;; mark takes an optional arg like the new Fmark_marker() does.  When 
  42. ;; the region is not active, mark returns nil unless the optional arg is true.
  43. ;;
  44. ;; push-mark, pop-mark, exchange-point-and-mark, and set-marker, and
  45. ;; set-mark-command use (mark t) so that they can access the mark whether
  46. ;; the region is active or not.  
  47. ;;
  48. ;; shell-command, shell-command-on-region, yank, and yank-pop (which all
  49. ;; push a mark) have been altered to call exchange-point-and-mark with an
  50. ;; argument, meaning "don't activate the region".  These commands  only use
  51. ;; exchange-point-and-mark to position the newly-pushed mark correctly, so
  52. ;; this isn't a user-visible change.  These functions have also been altered
  53. ;; to use (mark t) for the same reason.
  54.  
  55. ;; 97/3/14 Jareth Hein (jhod@po.iijnet.or.jp) added kinsoku processing (support
  56. ;; for filling of Asian text) into the fill code. This was ripped bleeding from
  57. ;; Mule-2.3, and could probably use some feature additions (like additional wrap
  58. ;; styles, etc)
  59.  
  60. ;; 97/06/11 Steve Baur (steve@altair.xemacs.org) Convert use of
  61. ;;  (preceding|following)-char to char-(after|before).
  62.  
  63. ;;; Code:
  64.  
  65. (defgroup editing-basics nil
  66.   "Most basic editing variables."
  67.   :group 'editing)
  68.  
  69. (defgroup killing nil
  70.   "Killing and yanking commands."
  71.   :group 'editing)
  72.  
  73. (defgroup fill-comments nil
  74.   "Indenting and filling of comments."
  75.   :prefix "comment-"
  76.   :group 'fill)
  77.  
  78. (defgroup paren-matching nil
  79.   "Highlight (un)matching of parens and expressions."
  80.   :prefix "paren-"
  81.   :group 'matching)
  82.  
  83. (defgroup log-message nil
  84.   "Messages logging and display customizations."
  85.   :group 'minibuffer)
  86.  
  87. (defgroup warnings nil
  88.   "Warnings customizations."
  89.   :group 'minibuffer)
  90.  
  91.  
  92. (defun newline (&optional arg)
  93.   "Insert a newline, and move to left margin of the new line if it's blank.
  94. The newline is marked with the text-property `hard'.
  95. With arg, insert that many newlines.
  96. In Auto Fill mode, if no numeric arg, break the preceding line if it's long."
  97.   (interactive "*P")
  98.   (barf-if-buffer-read-only nil (point))
  99.   ;; Inserting a newline at the end of a line produces better redisplay in
  100.   ;; try_window_id than inserting at the beginning of a line, and the textual
  101.   ;; result is the same.  So, if we're at beginning of line, pretend to be at
  102.   ;; the end of the previous line.
  103.   (let ((flag (and (not (bobp)) 
  104.            (bolp)
  105.            ;; Make sure the newline before point isn't intangible.
  106.            (not (get-char-property (1- (point)) 'intangible))
  107.            ;; Make sure the newline before point isn't read-only.
  108.            (not (get-char-property (1- (point)) 'read-only))
  109.            ;; Make sure the newline before point isn't invisible.
  110.            (not (get-char-property (1- (point)) 'invisible))
  111.            ;; This should probably also test for the previous char
  112.            ;;  being the *last* character too.
  113.            (not (get-char-property (1- (point)) 'end-open))
  114.            ;; Make sure the newline before point has the same
  115.            ;; properties as the char before it (if any).
  116.            (< (or (previous-extent-change (point)) -2) 
  117.               (- (point) 2))))
  118.     (was-page-start (and (bolp)
  119.                  (looking-at page-delimiter)))
  120.     (beforepos (point)))
  121.     (if flag (backward-char 1))
  122.     ;; Call self-insert so that auto-fill, abbrev expansion etc. happens.
  123.     ;; Set last-command-char to tell self-insert what to insert.
  124.     (let ((last-command-char ?\n)
  125.       ;; Don't auto-fill if we have a numeric argument.
  126.       ;; Also not if flag is true (it would fill wrong line);
  127.       ;; there is no need to since we're at BOL.
  128.       (auto-fill-function (if (or arg flag) nil auto-fill-function)))
  129.       (unwind-protect
  130.       (self-insert-command (prefix-numeric-value arg))
  131.     ;; If we get an error in self-insert-command, put point at right place.
  132.     (if flag (forward-char 1))))
  133.     ;; If we did *not* get an error, cancel that forward-char.
  134.     (if flag (backward-char 1))
  135.     ;; Mark the newline(s) `hard'.
  136.     (if use-hard-newlines
  137.     (let* ((from (- (point) (if arg (prefix-numeric-value arg) 1)))
  138.            (sticky (get-text-property from 'end-open))) ; XEmacs
  139.       (put-text-property from (point) 'hard 't)
  140.       ;; If end-open is not "t", add 'hard to end-open list
  141.       (if (and (listp sticky) (not (memq 'hard sticky)))
  142.           (put-text-property from (point) 'end-open ; XEmacs
  143.                  (cons 'hard sticky)))))
  144.     ;; If the newline leaves the previous line blank,
  145.     ;; and we have a left margin, delete that from the blank line.
  146.     (or flag
  147.     (save-excursion
  148.       (goto-char beforepos)
  149.       (beginning-of-line)
  150.       (and (looking-at "[ \t]$")
  151.            (> (current-left-margin) 0)
  152.            (delete-region (point) (progn (end-of-line) (point))))))
  153.     (if flag (forward-char 1))
  154.     ;; Indent the line after the newline, except in one case:
  155.     ;; when we added the newline at the beginning of a line
  156.     ;; which starts a page.
  157.     (or was-page-start
  158.     (move-to-left-margin nil t)))
  159.   nil)
  160.  
  161. (defun set-hard-newline-properties (from to)
  162.   (let ((sticky (get-text-property from 'rear-nonsticky)))
  163.     (put-text-property from to 'hard 't)
  164.     ;; If rear-nonsticky is not "t", add 'hard to rear-nonsticky list
  165.     (if (and (listp sticky) (not (memq 'hard sticky)))
  166.     (put-text-property from (point) 'rear-nonsticky
  167.                (cons 'hard sticky)))))
  168.  
  169. (defun open-line (arg)
  170.   "Insert a newline and leave point before it.
  171. If there is a fill prefix and/or a left-margin, insert them on the new line
  172. if the line would have been blank.
  173. With arg N, insert N newlines."
  174.   (interactive "*p")
  175.   (let* ((do-fill-prefix (and fill-prefix (bolp)))
  176.      (do-left-margin (and (bolp) (> (current-left-margin) 0)))
  177.      (loc (point)))
  178.     (newline arg)
  179.     (goto-char loc)
  180.     (while (> arg 0)
  181.       (cond ((bolp)
  182.          (if do-left-margin (indent-to (current-left-margin)))
  183.          (if do-fill-prefix (insert fill-prefix))))
  184.       (forward-line 1)
  185.       (setq arg (1- arg)))
  186.     (goto-char loc)
  187.     (end-of-line)))
  188.  
  189. (defun split-line ()
  190.   "Split current line, moving portion beyond point vertically down."
  191.   (interactive "*")
  192.   (skip-chars-forward " \t")
  193.   (let ((col (current-column))
  194.     (pos (point)))
  195.     (newline 1)
  196.     (indent-to col 0)
  197.     (goto-char pos)))
  198.  
  199. (defun quoted-insert (arg)
  200.   "Read next input character and insert it.
  201. This is useful for inserting control characters.
  202. You may also type up to 3 octal digits, to insert a character with that code.
  203.  
  204. In overwrite mode, this function inserts the character anyway, and
  205. does not handle octal digits specially.  This means that if you use
  206. overwrite as your normal editing mode, you can use this function to
  207. insert characters when necessary.
  208.  
  209. In binary overwrite mode, this function does overwrite, and octal
  210. digits are interpreted as a character code.  This is supposed to make
  211. this function useful in editing binary files."
  212.   (interactive "*p")
  213.   (let ((char (if (or (not overwrite-mode)
  214.               (eq overwrite-mode 'overwrite-mode-binary))
  215.           (read-quoted-char)
  216.         (read-char))))
  217.     (if (> arg 0)
  218.     (if (eq overwrite-mode 'overwrite-mode-binary)
  219.         (delete-char arg)))
  220.     (while (> arg 0)
  221.       (insert char)
  222.       (setq arg (1- arg)))))
  223.  
  224. (defun delete-indentation (&optional arg)
  225.   "Join this line to previous and fix up whitespace at join.
  226. If there is a fill prefix, delete it from the beginning of this line.
  227. With argument, join this line to following line."
  228.   (interactive "*P")
  229.   (beginning-of-line)
  230.   (if arg (forward-line 1))
  231.   (if (eq (char-before (point)) ?\n)
  232.       (progn
  233.     (delete-region (point) (1- (point)))
  234.     ;; If the second line started with the fill prefix,
  235.     ;; delete the prefix.
  236.     (if (and fill-prefix
  237.          (<= (+ (point) (length fill-prefix)) (point-max))
  238.          (string= fill-prefix
  239.               (buffer-substring (point)
  240.                         (+ (point) (length fill-prefix)))))
  241.         (delete-region (point) (+ (point) (length fill-prefix))))
  242.     (fixup-whitespace))))
  243.  
  244. (defun fixup-whitespace ()
  245.   "Fixup white space between objects around point.
  246. Leave one space or none, according to the context."
  247.   (interactive "*")
  248.   (save-excursion
  249.     (delete-horizontal-space)
  250.     (if (or (looking-at "^\\|\\s)")
  251.         (save-excursion (forward-char -1)
  252.                 (looking-at "$\\|\\s(\\|\\s'")))
  253.     nil
  254.       (insert ?\ ))))
  255.  
  256. (defun delete-horizontal-space ()
  257.   "Delete all spaces and tabs around point."
  258.   (interactive "*")
  259.   (skip-chars-backward " \t")
  260.   (delete-region (point) (progn (skip-chars-forward " \t") (point))))
  261.  
  262. (defun just-one-space ()
  263.   "Delete all spaces and tabs around point, leaving one space."
  264.   (interactive "*")
  265.   (if abbrev-mode ; XEmacs
  266.       (expand-abbrev))
  267.   (skip-chars-backward " \t")
  268.   (if (eq (char-after (point)) ? ) ; XEmacs
  269.       (forward-char 1)
  270.     (insert ? ))
  271.   (delete-region (point) (progn (skip-chars-forward " \t") (point))))
  272.  
  273. (defun delete-blank-lines ()
  274.   "On blank line, delete all surrounding blank lines, leaving just one.
  275. On isolated blank line, delete that one.
  276. On nonblank line, delete any immediately following blank lines."
  277.   (interactive "*")
  278.   (let (thisblank singleblank)
  279.     (save-excursion
  280.       (beginning-of-line)
  281.       (setq thisblank (looking-at "[ \t]*$"))
  282.       ;; Set singleblank if there is just one blank line here.
  283.       (setq singleblank
  284.         (and thisblank
  285.          (not (looking-at "[ \t]*\n[ \t]*$"))
  286.          (or (bobp)
  287.              (progn (forward-line -1)
  288.                 (not (looking-at "[ \t]*$")))))))
  289.     ;; Delete preceding blank lines, and this one too if it's the only one.
  290.     (if thisblank
  291.     (progn
  292.       (beginning-of-line)
  293.       (if singleblank (forward-line 1))
  294.       (delete-region (point)
  295.              (if (re-search-backward "[^ \t\n]" nil t)
  296.                  (progn (forward-line 1) (point))
  297.                (point-min)))))
  298.     ;; Delete following blank lines, unless the current line is blank
  299.     ;; and there are no following blank lines.
  300.     (if (not (and thisblank singleblank))
  301.     (save-excursion
  302.       (end-of-line)
  303.       (forward-line 1)
  304.       (delete-region (point)
  305.              (if (re-search-forward "[^ \t\n]" nil t)
  306.                  (progn (beginning-of-line) (point))
  307.                (point-max)))))
  308.     ;; Handle the special case where point is followed by newline and eob.
  309.     ;; Delete the line, leaving point at eob.
  310.     (if (looking-at "^[ \t]*\n\\'")
  311.     (delete-region (point) (point-max)))))
  312.  
  313. (defun back-to-indentation ()
  314.   "Move point to the first non-whitespace character on this line."
  315.   ;; XEmacs change
  316.   (interactive "_")
  317.   (beginning-of-line 1)
  318.   (skip-chars-forward " \t"))
  319.  
  320. (defun newline-and-indent ()
  321.   "Insert a newline, then indent according to major mode.
  322. Indentation is done using the value of `indent-line-function'.
  323. In programming language modes, this is the same as TAB.
  324. In some text modes, where TAB inserts a tab, this command indents to the
  325. column specified by the function `current-left-margin'."
  326.   (interactive "*")
  327.   (delete-region (point) (progn (skip-chars-backward " \t") (point)))
  328.   (newline)
  329.   (indent-according-to-mode))
  330.  
  331. (defun reindent-then-newline-and-indent ()
  332.   "Reindent current line, insert newline, then indent the new line.
  333. Indentation of both lines is done according to the current major mode,
  334. which means calling the current value of `indent-line-function'.
  335. In programming language modes, this is the same as TAB.
  336. In some text modes, where TAB inserts a tab, this indents to the
  337. column specified by the function `current-left-margin'."
  338.   (interactive "*")
  339.   (save-excursion
  340.     (delete-region (point) (progn (skip-chars-backward " \t") (point)))
  341.     (indent-according-to-mode))
  342.   (newline)
  343.   (indent-according-to-mode))
  344.  
  345. ;; Internal subroutine of delete-char
  346. (defun kill-forward-chars (arg)
  347.   (if (listp arg) (setq arg (car arg)))
  348.   (if (eq arg '-) (setq arg -1))
  349.   (kill-region (point) (+ (point) arg)))
  350.  
  351. ;; Internal subroutine of backward-delete-char
  352. (defun kill-backward-chars (arg)
  353.   (if (listp arg) (setq arg (car arg)))
  354.   (if (eq arg '-) (setq arg -1))
  355.   (kill-region (point) (- (point) arg)))
  356.  
  357. (defun backward-delete-char-untabify (arg &optional killp)
  358.   "Delete characters backward, changing tabs into spaces.
  359. Delete ARG chars, and kill (save in kill ring) if KILLP is non-nil.
  360. Interactively, ARG is the prefix arg (default 1)
  361. and KILLP is t if a prefix arg was specified."
  362.   (interactive "*p\nP")
  363.   (let ((count arg))
  364.     (save-excursion
  365.       (while (and (> count 0) (not (bobp)))
  366.     (if (eq (char-before (point)) ?\t) ; XEmacs
  367.         (let ((col (current-column)))
  368.           (forward-char -1)
  369.           (setq col (- col (current-column)))
  370.           (insert-char ?\ col)
  371.           (delete-char 1)))
  372.     (forward-char -1)
  373.     (setq count (1- count)))))
  374.   (delete-backward-char arg killp)
  375.   ;; XEmacs: In overwrite mode, back over columns while clearing them out,
  376.   ;; unless at end of line.
  377.   (and overwrite-mode (not (eolp))
  378.        (save-excursion (insert-char ?\  arg))))
  379.  
  380. (defcustom delete-key-deletes-forward nil
  381.   "*If non-nil, the DEL key will erase one character forwards.
  382. If nil, the DEL key will erase one character backwards."
  383.   :type 'boolean
  384.   :group 'editing-basics)
  385.  
  386. (defcustom backward-delete-function 'backward-delete-char
  387.   "*Function called to delete backwards on a delete keypress.
  388. If `delete-key-deletes-forward' is nil, `backward-or-forward-delete-char'
  389. calls this function to erase one character backwards.  Default value
  390. is 'backward-delete-char, with 'backward-delete-char-untabify being a
  391. popular alternate setting."
  392.   :type 'function
  393.   :group 'editing-basics)
  394.  
  395. (eval-when-compile
  396.   (defmacro delete-forward-p ()
  397.     '(and delete-key-deletes-forward
  398.       (or (eq 'tty (device-type))
  399.           (x-keysym-on-keyboard-sans-modifiers-p 'backspace)))))
  400.  
  401. (defun backward-or-forward-delete-char (arg)
  402.   "Delete either one character backwards or one character forwards.
  403. Controlled by the state of `delete-key-deletes-forward' and whether the
  404. BackSpace keysym even exists on your keyboard.  If you don't have a
  405. BackSpace keysym, the delete key should always delete one character
  406. backwards."
  407.   (interactive "*p")
  408.   (if (delete-forward-p)
  409.       (delete-char arg)
  410.     (funcall backward-delete-function arg)))
  411.  
  412. (defun backward-or-forward-kill-word (arg)
  413.   "Delete either one word backwards or one word forwards.
  414. Controlled by the state of `delete-key-deletes-forward' and whether the
  415. BackSpace keysym even exists on your keyboard.  If you don't have a
  416. BackSpace keysym, the delete key should always delete one character
  417. backwards."
  418.   (interactive "*p")
  419.   (if (delete-forward-p)
  420.       (kill-word arg)
  421.     (backward-kill-word arg)))
  422.  
  423. (defun backward-or-forward-kill-sentence (arg)
  424.     "Delete either one sentence backwards or one sentence forwards.
  425. Controlled by the state of `delete-key-deletes-forward' and whether the
  426. BackSpace keysym even exists on your keyboard.  If you don't have a
  427. BackSpace keysym, the delete key should always delete one character
  428. backwards."
  429.   (interactive "*P")
  430.   (if (delete-forward-p)
  431.       (kill-sentence arg)
  432.     (backward-kill-sentence (prefix-numeric-value arg))))
  433.  
  434. (defun backward-or-forward-kill-sexp (arg)
  435.     "Delete either one sexpr backwards or one sexpr forwards.
  436. Controlled by the state of `delete-key-deletes-forward' and whether the
  437. BackSpace keysym even exists on your keyboard.  If you don't have a
  438. BackSpace keysym, the delete key should always delete one character
  439. backwards."
  440.   (interactive "*p")
  441.   (if (delete-forward-p)
  442.       (kill-sexp arg)
  443.     (backward-kill-sexp arg)))
  444.  
  445. (defun zap-to-char (arg char)
  446.   "Kill up to and including ARG'th occurrence of CHAR.
  447. Goes backward if ARG is negative; error if CHAR not found."
  448.   (interactive "*p\ncZap to char: ")
  449.   (kill-region (point) (progn
  450.              (search-forward (char-to-string char) nil nil arg)
  451. ;             (goto-char (if (> arg 0) (1- (point)) (1+ (point))))
  452.              (point))))
  453.  
  454. (defun beginning-of-buffer (&optional arg)
  455.   "Move point to the beginning of the buffer; leave mark at previous position.
  456. With arg N, put point N/10 of the way from the beginning.
  457.  
  458. If the buffer is narrowed, this command uses the beginning and size
  459. of the accessible part of the buffer.
  460.  
  461. Don't use this command in Lisp programs!
  462. \(goto-char (point-min)) is faster and avoids clobbering the mark."
  463.   ;; XEmacs change
  464.   (interactive "_P")
  465.   (push-mark)
  466.   (let ((size (- (point-max) (point-min))))
  467.     (goto-char (if arg
  468.            (+ (point-min)
  469.               (if (> size 10000)
  470.               ;; Avoid overflow for large buffer sizes!
  471.               (* (prefix-numeric-value arg)
  472.                  (/ size 10))
  473.             (/ (+ 10 (* size (prefix-numeric-value arg))) 10)))
  474.          (point-min))))
  475.   (if arg (forward-line 1)))
  476.  
  477. (defun end-of-buffer (&optional arg)
  478.   "Move point to the end of the buffer; leave mark at previous position.
  479. With arg N, put point N/10 of the way from the end.
  480.  
  481. If the buffer is narrowed, this command uses the beginning and size
  482. of the accessible part of the buffer.
  483.  
  484. Don't use this command in Lisp programs!
  485. \(goto-char (point-max)) is faster and avoids clobbering the mark."
  486.   ;; XEmacs change
  487.   (interactive "_P")
  488.   (push-mark)
  489.   ;; XEmacs changes here.
  490.   (let ((scroll-to-end (not (pos-visible-in-window-p (point-max))))
  491.     (size (- (point-max) (point-min))))
  492.     (goto-char (if arg
  493.            (- (point-max)
  494.               (if (> size 10000)
  495.               ;; Avoid overflow for large buffer sizes!
  496.               (* (prefix-numeric-value arg)
  497.                  (/ size 10))
  498.             (/ (* size (prefix-numeric-value arg)) 10)))
  499.          (point-max)))
  500.     (cond (arg
  501.            ;; If we went to a place in the middle of the buffer,
  502.            ;; adjust it to the beginning of a line.
  503.            (forward-line 1))
  504.       ;; XEmacs change
  505.       (scroll-to-end
  506.            ;; If the end of the buffer is not already on the screen,
  507.            ;; then scroll specially to put it near, but not at, the bottom.
  508.            (recenter -3)))))
  509.  
  510. ;; XEmacs (not in FSF)
  511. (defun mark-beginning-of-buffer (&optional arg)
  512.   "Push a mark at the beginning of the buffer; leave point where it is.
  513. With arg N, push mark N/10 of the way from the true beginning."
  514.   (interactive "P")
  515.   (push-mark (if arg
  516.          (if (> (buffer-size) 10000)
  517.              ;; Avoid overflow for large buffer sizes!
  518.              (* (prefix-numeric-value arg)
  519.             (/ (buffer-size) 10))
  520.            (/ (+ 10 (* (buffer-size) (prefix-numeric-value arg))) 10))
  521.            (point-min))
  522.              nil
  523.              t))
  524. (define-function 'mark-bob 'mark-beginning-of-buffer)
  525.  
  526. ;; XEmacs (not in FSF)
  527. (defun mark-end-of-buffer (&optional arg)
  528.   "Push a mark at the end of the buffer; leave point where it is.
  529. With arg N, push mark N/10 of the way from the true end."
  530.   (interactive "P")
  531.   (push-mark (if arg
  532.          (- (1+ (buffer-size))
  533.             (if (> (buffer-size) 10000)
  534.             ;; Avoid overflow for large buffer sizes!
  535.             (* (prefix-numeric-value arg)
  536.                (/ (buffer-size) 10))
  537.               (/ (* (buffer-size) (prefix-numeric-value arg)) 10)))
  538.                  (point-max))
  539.              nil
  540.              t))
  541. (define-function 'mark-eob 'mark-end-of-buffer)
  542.  
  543. (defun mark-whole-buffer ()
  544.   "Put point at beginning and mark at end of buffer.
  545. You probably should not use this function in Lisp programs;
  546. it is usually a mistake for a Lisp function to use any subroutine
  547. that uses or sets the mark."
  548.   (interactive)
  549.   (push-mark (point))
  550.   (push-mark (point-max) nil t)
  551.   (goto-char (point-min)))
  552.  
  553. ;; XEmacs
  554. (defun eval-current-buffer (&optional printflag)
  555.   "Evaluate the current buffer as Lisp code.
  556. Programs can pass argument PRINTFLAG which controls printing of output:
  557. nil means discard it; anything else is stream for print."
  558.   (interactive)
  559.   (eval-buffer (current-buffer) printflag))
  560.  
  561. ;; XEmacs
  562. (defun count-words-buffer (b)
  563.   (interactive "b")
  564.   (save-excursion
  565.     (let ((buf (or b (current-buffer))))
  566.       (set-buffer buf)
  567.       (message "Buffer has %d words"
  568.            (count-words-region (point-min) (point-max))))))
  569.  
  570. ;; XEmacs
  571. (defun count-words-region (start end)
  572.   (interactive "r")
  573.   (save-excursion
  574.     (let ((n 0))
  575.       (goto-char start)
  576.       (while (< (point) end)
  577.     (if (forward-word 1)
  578.         (setq n (1+ n))))
  579.       (message "Region has %d words" n)
  580.       n)))
  581.  
  582. (defun count-lines-region (start end)
  583.   "Print number of lines and characters in the region."
  584.   ;; XEmacs change
  585.   (interactive "_r")
  586.   (message "Region has %d lines, %d characters"
  587.        (count-lines start end) (- end start)))
  588.  
  589. ;; XEmacs
  590. (defun count-lines-buffer (b)
  591.   "Print number of lines and characters in the specified buffer."
  592.   (interactive "_b")
  593.   (save-excursion
  594.     (let ((buf (or b (current-buffer)))
  595.           cnt)
  596.       (set-buffer buf)
  597.       (setq cnt (count-lines (point-min) (point-max)))
  598.       (message "Buffer has %d lines, %d characters"
  599.                cnt (- (point-max) (point-min)))
  600.       cnt)))
  601.  
  602. (defun what-line ()
  603.   "Print the current buffer line number and narrowed line number of point."
  604.   ;; XEmacs change
  605.   (interactive "_")
  606.   (let ((opoint (point)) start)
  607.     (save-excursion
  608.       (save-restriction
  609.     (goto-char (point-min))
  610.     (widen)
  611.     (beginning-of-line)
  612.     (setq start (point))
  613.     (goto-char opoint)
  614.     (beginning-of-line)
  615.     (if (/= start 1)
  616.         (message "line %d (narrowed line %d)"
  617.              (1+ (count-lines 1 (point)))
  618.              (1+ (count-lines start (point))))
  619.       (message "Line %d" (1+ (count-lines 1 (point)))))))))
  620.  
  621.  
  622. (defun count-lines (start end)
  623.   "Return number of lines between START and END.
  624. This is usually the number of newlines between them,
  625. but can be one more if START is not equal to END
  626. and the greater of them is not at the start of a line."
  627.   (save-excursion
  628.     (save-restriction
  629.       (narrow-to-region start end)
  630.       (goto-char (point-min))
  631.       (if (eq selective-display t)
  632.       (save-match-data
  633.         (let ((done 0))
  634.           (while (re-search-forward "[\n\C-m]" nil t 40)
  635.         (setq done (+ 40 done)))
  636.           (while (re-search-forward "[\n\C-m]" nil t 1)
  637.         (setq done (+ 1 done)))
  638.           (goto-char (point-max))
  639.           (if (and (/= start end)
  640.                (not (bolp)))
  641.           (1+ done)
  642.         done)))
  643.     (- (buffer-size) (forward-line (buffer-size)))))))
  644.  
  645. (defun what-cursor-position ()
  646.   "Print info on cursor position (on screen and within buffer)."
  647.   ;; XEmacs change
  648.   (interactive "_")
  649.   (let* ((char (char-after (point))) ; XEmacs
  650.      (beg (point-min))
  651.      (end (point-max))
  652.          (pos (point))
  653.      (total (buffer-size))
  654.      (percent (if (> total 50000)
  655.               ;; Avoid overflow from multiplying by 100!
  656.               (/ (+ (/ total 200) (1- pos)) (max (/ total 100) 1))
  657.             (/ (+ (/ total 2) (* 100 (1- pos))) (max total 1))))
  658.      (hscroll (if (= (window-hscroll) 0)
  659.               ""
  660.             (format " Hscroll=%d" (window-hscroll))))
  661.      (col (current-column)))
  662.     (if (= pos end)
  663.     (if (or (/= beg 1) (/= end (1+ total)))
  664.         (message "point=%d of %d(%d%%) <%d - %d>  column %d %s"
  665.              pos total percent beg end col hscroll)
  666.       (message "point=%d of %d(%d%%)  column %d %s"
  667.            pos total percent col hscroll))
  668.       ;; XEmacs: don't use single-key-description
  669.       (if (or (/= beg 1) (/= end (1+ total)))
  670.       (message "Char: %s (0%o, %d, 0x%x)  point=%d of %d(%d%%) <%d - %d>  column %d %s"
  671.            (text-char-description char) char char char pos total
  672.            percent beg end col hscroll)
  673.     (message "Char: %s (0%o, %d, 0x%x)  point=%d of %d(%d%%)  column %d %s"
  674.          (text-char-description char) char char char pos total
  675.          percent col hscroll)))))
  676.  
  677. (defun fundamental-mode ()
  678.   "Major mode not specialized for anything in particular.
  679. Other major modes are defined by comparison with this one."
  680.   (interactive)
  681.   (kill-all-local-variables))
  682.  
  683. ;; XEmacs the following are declared elsewhere
  684. ;(defvar read-expression-map (cons 'keymap minibuffer-local-map)
  685. ;  "Minibuffer keymap used for reading Lisp expressions.")
  686. ;(define-key read-expression-map "\M-\t" 'lisp-complete-symbol)
  687.  
  688. ;(put 'eval-expression 'disabled t)
  689.  
  690. ;(defvar read-expression-history nil)
  691.  
  692. ;; We define this, rather than making `eval' interactive,
  693. ;; for the sake of completion of names like eval-region, eval-current-buffer.
  694. (defun eval-expression (expression)
  695.   "Evaluate EXPRESSION and print value in minibuffer.
  696. Value is also consed on to front of the variable `values'."
  697.   ;(interactive "xEval: ")
  698.   (interactive
  699.    (list (read-from-minibuffer "Eval: "
  700.                    nil read-expression-map t
  701.                    'read-expression-history)))
  702.   (setq values (cons (eval expression) values))
  703.   (prin1 (car values) t))
  704.  
  705. ;; XEmacs -- extra parameter (variant, but equivalent logic)
  706. (defun edit-and-eval-command (prompt command &optional history)
  707.   "Prompting with PROMPT, let user edit COMMAND and eval result.
  708. COMMAND is a Lisp expression.  Let user edit that expression in
  709. the minibuffer, then read and evaluate the result."
  710.   (let ((command (read-expression prompt
  711.                   ;; first try to format the thing readably;
  712.                   ;; and if that fails, print it normally.
  713.                   (condition-case ()
  714.                       (let ((print-readably t))
  715.                     (prin1-to-string command))
  716.                     (error (prin1-to-string command)))
  717.                   (or history '(command-history . 1)))))
  718.     (or history (setq history 'command-history))
  719.     (if (consp history)
  720.     (setq history (car history)))
  721.     (if (eq history t)
  722.     nil
  723.       ;; If command was added to the history as a string,
  724.       ;; get rid of that.  We want only evallable expressions there.
  725.       (if (stringp (car (symbol-value history)))
  726.       (set history (cdr (symbol-value history))))
  727.  
  728.       ;; If command to be redone does not match front of history,
  729.       ;; add it to the history.
  730.       (or (equal command (car (symbol-value history)))
  731.       (set history (cons command (symbol-value history)))))
  732.     (eval command)))
  733.  
  734. (defun repeat-complex-command (arg)
  735.   "Edit and re-evaluate last complex command, or ARGth from last.
  736. A complex command is one which used the minibuffer.
  737. The command is placed in the minibuffer as a Lisp form for editing.
  738. The result is executed, repeating the command as changed.
  739. If the command has been changed or is not the most recent previous command
  740. it is added to the front of the command history.
  741. You can use the minibuffer history commands \\<minibuffer-local-map>\\[next-history-element] and \\[previous-history-element]
  742. to get different commands to edit and resubmit."
  743.   (interactive "p")
  744.   ;; XEmacs: It looks like our version is better -sb
  745.   (let ((print-level nil))
  746.     (edit-and-eval-command "Redo: "
  747.                (or (nth (1- arg) command-history)
  748.                    (error ""))
  749.                (cons 'command-history arg))))
  750.  
  751. ;; XEmacs: Functions moved to minibuf.el
  752. ;; previous-matching-history-element
  753. ;; next-matching-history-element
  754. ;; next-history-element
  755. ;; previous-history-element
  756. ;; next-complete-history-element
  757. ;; previous-complete-history-element
  758.  
  759. (defun goto-line (arg)
  760.   "Goto line ARG, counting from line 1 at beginning of buffer."
  761.   (interactive "NGoto line: ")
  762.   (setq arg (prefix-numeric-value arg))
  763.   (save-restriction
  764.     (widen)
  765.     (goto-char 1)
  766.     (if (eq selective-display t)
  767.     (re-search-forward "[\n\C-m]" nil 'end (1- arg))
  768.       (forward-line (1- arg)))))
  769.  
  770. ;Put this on C-x u, so we can force that rather than C-_ into startup msg
  771. (define-function 'advertised-undo 'undo)
  772.  
  773. (defun undo (&optional arg)
  774.   "Undo some previous changes.
  775. Repeat this command to undo more changes.
  776. A numeric argument serves as a repeat count."
  777.   (interactive "*p")
  778.   ;; If we don't get all the way through, make last-command indicate that
  779.   ;; for the following command.
  780.   (setq this-command t)
  781.   (let ((modified (buffer-modified-p))
  782.     (recent-save (recent-auto-save-p)))
  783.     (or (eq (selected-window) (minibuffer-window))
  784.     (display-message 'command "Undo!"))
  785.     (or (and (eq last-command 'undo)
  786.          (eq (current-buffer) last-undo-buffer)) ; XEmacs
  787.     (progn (undo-start)
  788.            (undo-more 1)))
  789.     (undo-more (or arg 1))
  790.     ;; Don't specify a position in the undo record for the undo command.
  791.     ;; Instead, undoing this should move point to where the change is.
  792.     (let ((tail buffer-undo-list)
  793.       done)
  794.       (while (and tail (not done) (not (null (car tail))))
  795.     (if (integerp (car tail))
  796.         (progn
  797.           (setq done t)
  798.           (setq buffer-undo-list (delq (car tail) buffer-undo-list))))
  799.     (setq tail (cdr tail))))
  800.     (and modified (not (buffer-modified-p))
  801.      (delete-auto-save-file-if-necessary recent-save)))
  802.   ;; If we do get all the way through, make this-command indicate that.
  803.   (setq this-command 'undo))
  804.  
  805. (defvar pending-undo-list nil
  806.   "Within a run of consecutive undo commands, list remaining to be undone.")
  807.  
  808. (defvar last-undo-buffer nil)    ; XEmacs
  809.  
  810. (defun undo-start ()
  811.   "Set `pending-undo-list' to the front of the undo list.
  812. The next call to `undo-more' will undo the most recently made change."
  813.   (if (eq buffer-undo-list t)
  814.       (error "No undo information in this buffer"))
  815.   (setq pending-undo-list buffer-undo-list))
  816.  
  817. (defun undo-more (count)
  818.   "Undo back N undo-boundaries beyond what was already undone recently.
  819. Call `undo-start' to get ready to undo recent changes,
  820. then call `undo-more' one or more times to undo them."
  821.   (or pending-undo-list
  822.       (error "No further undo information"))
  823.   (setq pending-undo-list (primitive-undo count pending-undo-list)
  824.     last-undo-buffer (current-buffer)))    ; XEmacs
  825.  
  826. ;; XEmacs
  827. (defun call-with-transparent-undo (fn &rest args)
  828.   "Apply FN to ARGS, and then undo all changes made by FN to the current
  829. buffer.  The undo records are processed even if FN returns non-locally.
  830. There is no trace of the changes made by FN in the buffer's undo history.
  831.  
  832. You can use this in a write-file-hooks function with continue-save-buffer
  833. to make the contents of a disk file differ from its in-memory buffer."
  834.   (let ((buffer-undo-list nil)
  835.     ;; Kludge to prevent undo list truncation:
  836.     (undo-high-threshold -1)
  837.     (undo-threshold -1)
  838.     (obuffer (current-buffer)))
  839.     (unwind-protect
  840.     (apply fn args)
  841.       ;; Go to the buffer we will restore and make it writable:
  842.       (set-buffer obuffer)
  843.       (save-excursion
  844.     (let ((buffer-read-only nil))
  845.       (save-restriction
  846.         (widen)
  847.         ;; Perform all undos, with further undo logging disabled:
  848.         (let ((tail buffer-undo-list))
  849.           (setq buffer-undo-list t)
  850.           (while tail
  851.         (setq tail (primitive-undo (length tail) tail))))))))))
  852.  
  853. ;; XEmacs: The following are in other files
  854. ;; shell-command-history
  855. ;; shell-command-switch
  856. ;; shell-command
  857. ;; shell-command-sentinel
  858.  
  859.  
  860. (defconst universal-argument-map
  861.   (let ((map (make-sparse-keymap)))
  862.     (set-keymap-default-binding map 'universal-argument-other-key)
  863.     ;FSFmacs (define-key map [switch-frame] nil)
  864.     (define-key map [(t)] 'universal-argument-other-key)
  865.     (define-key map [(meta t)] 'universal-argument-other-key)
  866.     (define-key map [(control u)] 'universal-argument-more)
  867.     (define-key map [?-] 'universal-argument-minus)
  868.     (define-key map [?0] 'digit-argument)
  869.     (define-key map [?1] 'digit-argument)
  870.     (define-key map [?2] 'digit-argument)
  871.     (define-key map [?3] 'digit-argument)
  872.     (define-key map [?4] 'digit-argument)
  873.     (define-key map [?5] 'digit-argument)
  874.     (define-key map [?6] 'digit-argument)
  875.     (define-key map [?7] 'digit-argument)
  876.     (define-key map [?8] 'digit-argument)
  877.     (define-key map [?9] 'digit-argument)
  878.     map)
  879.   "Keymap used while processing \\[universal-argument].")
  880.  
  881. (defvar universal-argument-num-events nil
  882.   "Number of argument-specifying events read by `universal-argument'.
  883. `universal-argument-other-key' uses this to discard those events
  884. from (this-command-keys), and reread only the final command.")
  885.  
  886. (defun universal-argument ()
  887.   "Begin a numeric argument for the following command.
  888. Digits or minus sign following \\[universal-argument] make up the numeric argument.
  889. \\[universal-argument] following the digits or minus sign ends the argument.
  890. \\[universal-argument] without digits or minus sign provides 4 as argument.
  891. Repeating \\[universal-argument] without digits or minus sign
  892.  multiplies the argument by 4 each time."
  893.   (interactive)
  894.   (setq prefix-arg (list 4))
  895.   (setq zmacs-region-stays t)    ; XEmacs
  896.   (setq universal-argument-num-events (length (this-command-keys)))
  897.   (setq overriding-terminal-local-map universal-argument-map))
  898.  
  899. ;; A subsequent C-u means to multiply the factor by 4 if we've typed
  900. ;; nothing but C-u's; otherwise it means to terminate the prefix arg.
  901. (defun universal-argument-more (arg)
  902.   (interactive "_P")            ; XEmacs
  903.   (if (consp arg)
  904.       (setq prefix-arg (list (* 4 (car arg))))
  905.     (setq prefix-arg arg)
  906.     (setq overriding-terminal-local-map nil))
  907.   (setq universal-argument-num-events (length (this-command-keys))))
  908.  
  909. (defun negative-argument (arg)
  910.   "Begin a negative numeric argument for the next command.
  911. \\[universal-argument] following digits or minus sign ends the argument."
  912.   (interactive "_P")            ; XEmacs
  913.   (cond ((integerp arg)
  914.       (setq prefix-arg (- arg)))
  915.      ((eq arg '-)
  916.       (setq prefix-arg nil))
  917.      (t
  918.       (setq prefix-arg '-)))
  919.   (setq universal-argument-num-events (length (this-command-keys)))
  920.   (setq overriding-terminal-local-map universal-argument-map))
  921.  
  922. ;; XEmacs:  This function not synched with FSF
  923. (defun digit-argument (arg)
  924.   "Part of the numeric argument for the next command.
  925. \\[universal-argument] following digits or minus sign ends the argument."
  926.   (interactive "_P")            ; XEmacs
  927.   (let* ((event last-command-event)
  928.      (key (and (key-press-event-p event)
  929.            (event-key event)))
  930.      (digit (and key (characterp key) (>= key ?0) (<= key ?9)
  931.              (- key ?0))))
  932.     (if (null digit)
  933.     (universal-argument-other-key arg)
  934.       (cond ((integerp arg)
  935.          (setq prefix-arg (+ (* arg 10)
  936.                  (if (< arg 0) (- digit) digit))))
  937.         ((eq arg '-)
  938.          ;; Treat -0 as just -, so that -01 will work.
  939.          (setq prefix-arg (if (zerop digit) '- (- digit))))
  940.         (t
  941.          (setq prefix-arg digit)))
  942.       (setq universal-argument-num-events (length (this-command-keys)))
  943.       (setq overriding-terminal-local-map universal-argument-map))))
  944.  
  945. ;; For backward compatibility, minus with no modifiers is an ordinary
  946. ;; command if digits have already been entered.
  947. (defun universal-argument-minus (arg)
  948.   (interactive "_P") ; XEmacs
  949.   (if (integerp arg)
  950.       (universal-argument-other-key arg)
  951.     (negative-argument arg)))
  952.  
  953. ;; Anything else terminates the argument and is left in the queue to be
  954. ;; executed as a command.
  955. (defun universal-argument-other-key (arg)
  956.   (interactive "_P")            ; XEmacs
  957.   (setq prefix-arg arg)
  958.   (let* ((key (this-command-keys))
  959.      ;; FSF calls silly function `listify-key-sequence' here.
  960.       (keylist (append key nil)))
  961.     (setq unread-command-events
  962.        (append (nthcdr universal-argument-num-events keylist)
  963.            unread-command-events)))
  964.   (reset-this-command-lengths)
  965.   (setq overriding-terminal-local-map nil))
  966.  
  967.  
  968. ;; XEmacs -- keep zmacs-region active.
  969. (defun forward-to-indentation (arg)
  970.   "Move forward ARG lines and position at first nonblank character."
  971.   (interactive "_p")
  972.   (forward-line arg)
  973.   (skip-chars-forward " \t"))
  974.  
  975. (defun backward-to-indentation (arg)
  976.   "Move backward ARG lines and position at first nonblank character."
  977.   (interactive "_p")
  978.   (forward-line (- arg))
  979.   (skip-chars-forward " \t"))
  980.  
  981. (defcustom kill-whole-line nil
  982.   "*If non-nil, `kill-line' with no arg at beg of line kills the whole line."
  983.   :type 'boolean
  984.   :group 'killing)
  985.  
  986. (defun kill-line (&optional arg)
  987.   "Kill the rest of the current line; if no nonblanks there, kill thru newline.
  988. With prefix argument, kill that many lines from point.
  989. Negative arguments kill lines backward.
  990.  
  991. When calling from a program, nil means \"no arg\",
  992. a number counts as a prefix arg.
  993.  
  994. If `kill-whole-line' is non-nil, then kill the whole line
  995. when given no argument at the beginning of a line."
  996.   (interactive "*P")
  997.   (kill-region (point)
  998.            ;; Don't shift point before doing the delete; that way,
  999.            ;; undo will record the right position of point.
  1000. ;; FSF
  1001. ;           ;; It is better to move point to the other end of the kill
  1002. ;           ;; before killing.  That way, in a read-only buffer, point
  1003. ;           ;; moves across the text that is copied to the kill ring.
  1004. ;           ;; The choice has no effect on undo now that undo records
  1005. ;           ;; the value of point from before the command was run.
  1006. ;              (progn
  1007.            (save-excursion
  1008.          (if arg
  1009.              (forward-line (prefix-numeric-value arg))
  1010.            (if (eobp)
  1011.                (signal 'end-of-buffer nil))
  1012.            (if (or (looking-at "[ \t]*$") (and kill-whole-line (bolp)))
  1013.                (forward-line 1)
  1014.              (end-of-line)))
  1015.          (point))))
  1016.  
  1017. ;; XEmacs
  1018. (defun backward-kill-line nil
  1019.   "Kill back to the beginning of the line."
  1020.   (interactive)
  1021.   (let ((point (point)))
  1022.     (beginning-of-line nil)
  1023.     (kill-region (point) point)))
  1024.  
  1025.  
  1026. ;;;; Window system cut and paste hooks.
  1027. ;;;
  1028. ;;; I think that kill-hooks is a better name and more general mechanism
  1029. ;;; than interprogram-cut-function (from FSFmacs).  I don't like the behavior
  1030. ;;; of interprogram-paste-function: ^Y should always come from the kill ring,
  1031. ;;; not the X selection.  But if that were provided, it should be called (and
  1032. ;;; behave as) yank-hooks instead.  -- jwz
  1033.  
  1034. ;; [... code snipped ...]
  1035.  
  1036. (defcustom kill-hooks nil
  1037.   "*Functions run when something is added to the XEmacs kill ring.
  1038. These functions are called with one argument, the string most recently
  1039. cut or copied.  You can use this to, for example, make the most recent 
  1040. kill become the X Clipboard selection."
  1041.   :type 'hook
  1042.   :group 'killing)
  1043.  
  1044. ;;; `kill-hooks' seems not sufficient because
  1045. ;;; `interprogram-cut-function' requires more variable about to rotate
  1046. ;;; the cut buffers.  I'm afraid to change interface of `kill-hooks',
  1047. ;;; so I add it. (1997-11-03 by MORIOKA Tomohiko)
  1048.  
  1049. (defvar interprogram-cut-function nil
  1050.   "Function to call to make a killed region available to other programs.
  1051.  
  1052. Most window systems provide some sort of facility for cutting and
  1053. pasting text between the windows of different programs.
  1054. This variable holds a function that Emacs calls whenever text
  1055. is put in the kill ring, to make the new kill available to other
  1056. programs.
  1057.  
  1058. The function takes one or two arguments.
  1059. The first argument, TEXT, is a string containing
  1060. the text which should be made available.
  1061. The second, PUSH, if non-nil means this is a \"new\" kill;
  1062. nil means appending to an \"old\" kill.")
  1063.  
  1064. (defvar interprogram-paste-function nil
  1065.   "Function to call to get text cut from other programs.
  1066.  
  1067. Most window systems provide some sort of facility for cutting and
  1068. pasting text between the windows of different programs.
  1069. This variable holds a function that Emacs calls to obtain
  1070. text that other programs have provided for pasting.
  1071.  
  1072. The function should be called with no arguments.  If the function
  1073. returns nil, then no other program has provided such text, and the top
  1074. of the Emacs kill ring should be used.  If the function returns a
  1075. string, that string should be put in the kill ring as the latest kill.
  1076.  
  1077. Note that the function should return a string only if a program other
  1078. than Emacs has provided a string for pasting; if Emacs provided the
  1079. most recent string, the function should return nil.  If it is
  1080. difficult to tell whether Emacs or some other program provided the
  1081. current string, it is probably good enough to return nil if the string
  1082. is equal (according to `string=') to the last text Emacs provided.")
  1083.  
  1084.  
  1085. ;;;; The kill ring data structure.
  1086.  
  1087. (defvar kill-ring nil
  1088.   "List of killed text sequences.
  1089. Since the kill ring is supposed to interact nicely with cut-and-paste
  1090. facilities offered by window systems, use of this variable should
  1091. interact nicely with `interprogram-cut-function' and
  1092. `interprogram-paste-function'.  The functions `kill-new',
  1093. `kill-append', and `current-kill' are supposed to implement this
  1094. interaction; you may want to use them instead of manipulating the kill
  1095. ring directly.")
  1096.  
  1097. (defcustom kill-ring-max 30
  1098.   "*Maximum length of kill ring before oldest elements are thrown away."
  1099.   :type 'integer
  1100.   :group 'killing)
  1101.  
  1102. (defvar kill-ring-yank-pointer nil
  1103.   "The tail of the kill ring whose car is the last thing yanked.")
  1104.  
  1105. (defun kill-new (string &optional replace)
  1106.   "Make STRING the latest kill in the kill ring.
  1107. Set the kill-ring-yank pointer to point to it.
  1108. Run `kill-hooks'.
  1109. Optional second argument REPLACE non-nil means that STRING will replace
  1110. the front of the kill ring, rather than being added to the list."
  1111. ;  (and (fboundp 'menu-bar-update-yank-menu)
  1112. ;       (menu-bar-update-yank-menu string (and replace (car kill-ring))))
  1113.   (if replace
  1114.       (setcar kill-ring string)
  1115.     (setq kill-ring (cons string kill-ring))
  1116.     (if (> (length kill-ring) kill-ring-max)
  1117.     (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil)))
  1118.   (setq kill-ring-yank-pointer kill-ring)
  1119.   (if interprogram-cut-function
  1120.       (funcall interprogram-cut-function string (not replace)))
  1121.   (run-hook-with-args 'kill-hooks string))
  1122.  
  1123. (defun kill-append (string before-p)
  1124.   "Append STRING to the end of the latest kill in the kill ring.
  1125. If BEFORE-P is non-nil, prepend STRING to the kill.
  1126. Run `kill-hooks'."
  1127.   (kill-new (if before-p
  1128.         (concat string (car kill-ring))
  1129.           (concat (car kill-ring) string)) t))
  1130.  
  1131. (defun current-kill (n &optional do-not-move)
  1132.   "Rotate the yanking point by N places, and then return that kill.
  1133. If N is zero, `interprogram-paste-function' is set, and calling it
  1134. returns a string, then that string is added to the front of the
  1135. kill ring and returned as the latest kill.
  1136. If optional arg DO-NOT-MOVE is non-nil, then don't actually move the 
  1137. yanking point\; just return the Nth kill forward."
  1138.   (let ((interprogram-paste (and (= n 0)
  1139.                  interprogram-paste-function
  1140.                  (funcall interprogram-paste-function))))
  1141.     (if interprogram-paste
  1142.     (progn
  1143.       ;; Disable the interprogram cut function when we add the new
  1144.       ;; text to the kill ring, so Emacs doesn't try to own the
  1145.       ;; selection, with identical text.
  1146.       (let ((interprogram-cut-function nil))
  1147.         (kill-new interprogram-paste))
  1148.       interprogram-paste)
  1149.       (or kill-ring (error "Kill ring is empty"))
  1150.       (let* ((tem (nthcdr (mod (- n (length kill-ring-yank-pointer))
  1151.                    (length kill-ring))
  1152.               kill-ring)))
  1153.     (or do-not-move
  1154.         (setq kill-ring-yank-pointer tem))
  1155.     (car tem)))))
  1156.  
  1157.  
  1158.  
  1159. ;;;; Commands for manipulating the kill ring.
  1160.  
  1161. ;; In FSF killing read-only text just pastes it into kill-ring.  Which
  1162. ;; is a very bad idea -- see Jamie's comment below.
  1163.  
  1164. ;(defvar kill-read-only-ok nil
  1165. ;  "*Non-nil means don't signal an error for killing read-only text.")
  1166.  
  1167. (defun kill-region (beg end &optional verbose) ; verbose is XEmacs addition
  1168.   "Kill between point and mark.
  1169. The text is deleted but saved in the kill ring.
  1170. The command \\[yank] can retrieve it from there.
  1171. \(If you want to kill and then yank immediately, use \\[copy-region-as-kill].)
  1172.  
  1173. This is the primitive for programs to kill text (as opposed to deleting it).
  1174. Supply two arguments, character numbers indicating the stretch of text
  1175.  to be killed.
  1176. Any command that calls this function is a \"kill command\".
  1177. If the previous command was also a kill command,
  1178. the text killed this time appends to the text killed last time
  1179. to make one entry in the kill ring."
  1180.   (interactive "*r\np")
  1181. ;  (interactive
  1182. ;   (let ((region-hack (and zmacs-regions (eq last-command 'yank))))
  1183. ;     ;; This lets "^Y^W" work.  I think this is dumb, but zwei did it.
  1184. ;     (if region-hack (zmacs-activate-region))
  1185. ;     (prog1
  1186. ;     (list (point) (mark) current-prefix-arg)
  1187. ;       (if region-hack (zmacs-deactivate-region)))))
  1188.   ;; beg and end can be markers but the rest of this function is
  1189.   ;; written as if they are only integers
  1190.   (if (markerp beg) (setq beg (marker-position beg)))
  1191.   (if (markerp end) (setq end (marker-position end)))
  1192.   (or (and beg end) (if zmacs-regions ;; rewritten for I18N3 snarfing
  1193.             (error "The region is not active now")
  1194.               (error "The mark is not set now")))
  1195.   (if verbose (if buffer-read-only
  1196.           (display-message
  1197.            'command
  1198.            (format "Copying %d characters"
  1199.                (- (max beg end) (min beg end))))
  1200.         (display-message
  1201.          'command
  1202.          (format "Killing %d characters"
  1203.              (- (max beg end) (min beg end))))))
  1204.   (cond
  1205.  
  1206.    ;; I don't like this large change in behavior -- jwz
  1207.    ;; Read-Only text means it shouldn't be deleted, so I'm restoring
  1208.    ;; this code, but only for text-properties and not full extents. -sb
  1209.    ;; If the buffer is read-only, we should beep, in case the person
  1210.    ;; just isn't aware of this.  However, there's no harm in putting
  1211.    ;; the region's text in the kill ring, anyway.
  1212.    ((or (and buffer-read-only (not inhibit-read-only))
  1213.     (text-property-not-all (min beg end) (max beg end) 'read-only nil))
  1214.    ;; This is redundant.
  1215.    ;; (if verbose (message "Copying %d characters"
  1216.    ;;             (- (max beg end) (min beg end))))
  1217.     (copy-region-as-kill beg end)
  1218.    ;; ;; This should always barf, and give us the correct error.
  1219.    ;; (if kill-read-only-ok
  1220.    ;;      (message "Read only text copied to kill ring")
  1221.     (setq this-command 'kill-region)
  1222.     (barf-if-buffer-read-only)
  1223.     (signal 'buffer-read-only (list (current-buffer))))
  1224.  
  1225.    ;; In certain cases, we can arrange for the undo list and the kill
  1226.    ;; ring to share the same string object.  This code does that.
  1227.    ((not (or (eq buffer-undo-list t)
  1228.          (eq last-command 'kill-region)
  1229.          ;; Use = since positions may be numbers or markers.
  1230.          (= beg end)))
  1231.     ;; Don't let the undo list be truncated before we can even access it.
  1232.     ;; FSF calls this `undo-strong-limit'
  1233.     (let ((undo-high-threshold (+ (- end beg) 100))
  1234.       ;(old-list buffer-undo-list)
  1235.       tail)
  1236.       (delete-region beg end)
  1237.       ;; Search back in buffer-undo-list for this string,
  1238.       ;; in case a change hook made property changes.
  1239.       (setq tail buffer-undo-list)
  1240.       (while (and tail
  1241.           (not (stringp (car-safe (car-safe tail))))) ; XEmacs
  1242.     (pop tail))
  1243.       ;; Take the same string recorded for undo
  1244.       ;; and put it in the kill-ring.
  1245.       (and tail
  1246.        (kill-new (car (car tail))))))
  1247.  
  1248.    (t
  1249.     ;; if undo is not kept, grab the string then delete it (which won't
  1250.     ;; add another string to the undo list).
  1251.     (copy-region-as-kill beg end)
  1252.     (delete-region beg end)))
  1253.   (setq this-command 'kill-region))
  1254.  
  1255. ;; copy-region-as-kill no longer sets this-command, because it's confusing
  1256. ;; to get two copies of the text when the user accidentally types M-w and
  1257. ;; then corrects it with the intended C-w.
  1258. (defun copy-region-as-kill (beg end)
  1259.   "Save the region as if killed, but don't kill it.
  1260. Run `kill-hooks'."
  1261.   (interactive "r")
  1262.   (if (eq last-command 'kill-region)
  1263.       (kill-append (buffer-substring beg end) (< end beg))
  1264.     (kill-new (buffer-substring beg end)))
  1265.   nil)
  1266.  
  1267. (defun kill-ring-save (beg end)
  1268.   "Save the region as if killed, but don't kill it.
  1269. This command is similar to `copy-region-as-kill', except that it gives
  1270. visual feedback indicating the extent of the region being copied."
  1271.   (interactive "r")
  1272.   (copy-region-as-kill beg end)
  1273.   ;; copy before delay, for xclipboard's benefit
  1274.   (if (interactive-p)
  1275.       (let ((other-end (if (= (point) beg) end beg))
  1276.         (opoint (point))
  1277.         ;; Inhibit quitting so we can make a quit here
  1278.         ;; look like a C-g typed as a command.
  1279.         (inhibit-quit t))
  1280.     (if (pos-visible-in-window-p other-end (selected-window))
  1281.         (progn
  1282.           ;; FSF (I'm not sure what this does -sb)
  1283. ;          ;; Swap point and mark.
  1284. ;          (set-marker (mark-marker) (point) (current-buffer))
  1285.           (goto-char other-end)
  1286.               (sit-for 1)
  1287. ;          ;; Swap back.
  1288. ;          (set-marker (mark-marker) other-end (current-buffer))
  1289.               (goto-char opoint)
  1290.               ;; If user quit, deactivate the mark
  1291.           ;; as C-g would as a command.
  1292.           (and quit-flag (mark)
  1293.                    (zmacs-deactivate-region)))
  1294.       ;; too noisy. -- jwz
  1295. ;      (let* ((killed-text (current-kill 0))
  1296. ;         (message-len (min (length killed-text) 40)))
  1297. ;        (if (= (point) beg)
  1298. ;        ;; Don't say "killed"; that is misleading.
  1299. ;        (message "Saved text until \"%s\""
  1300. ;            (substring killed-text (- message-len)))
  1301. ;          (message "Saved text from \"%s\""
  1302. ;              (substring killed-text 0 message-len))))
  1303.       ))))
  1304.  
  1305. (defun append-next-kill ()
  1306.   "Cause following command, if it kills, to append to previous kill."
  1307.   ;; XEmacs
  1308.   (interactive "_")
  1309.   (if (interactive-p)
  1310.       (progn
  1311.     (setq this-command 'kill-region)
  1312.     (display-message 'command
  1313.              "If the next command is a kill, it will append"))
  1314.     (setq last-command 'kill-region)))
  1315.  
  1316. (defun yank-pop (arg)
  1317.   "Replace just-yanked stretch of killed text with a different stretch.
  1318. This command is allowed only immediately after a `yank' or a `yank-pop'.
  1319. At such a time, the region contains a stretch of reinserted
  1320. previously-killed text.  `yank-pop' deletes that text and inserts in its
  1321. place a different stretch of killed text.
  1322.  
  1323. With no argument, the previous kill is inserted.
  1324. With argument N, insert the Nth previous kill.
  1325. If N is negative, this is a more recent kill.
  1326.  
  1327. The sequence of kills wraps around, so that after the oldest one
  1328. comes the newest one."
  1329.   (interactive "*p")
  1330.   (if (not (eq last-command 'yank))
  1331.       (error "Previous command was not a yank"))
  1332.   (setq this-command 'yank)
  1333.   (let ((inhibit-read-only t)
  1334.     (before (< (point) (mark t))))
  1335.     (delete-region (point) (mark t))
  1336.     ;;(set-marker (mark-marker) (point) (current-buffer))
  1337.     (set-mark (point))
  1338.     (insert (current-kill arg))
  1339.     (if before
  1340.     ;; This is like exchange-point-and-mark, but doesn't activate the mark.
  1341.     ;; It is cleaner to avoid activation, even though the command
  1342.     ;; loop would deactivate the mark because we inserted text.
  1343.     (goto-char (prog1 (mark t)
  1344.              (set-marker (mark-marker t) (point) (current-buffer))))))
  1345.   nil)
  1346.  
  1347.  
  1348. (defun yank (&optional arg)
  1349.   "Reinsert the last stretch of killed text.
  1350. More precisely, reinsert the stretch of killed text most recently
  1351. killed OR yanked.  Put point at end, and set mark at beginning.
  1352. With just C-u as argument, same but put point at beginning (and mark at end).
  1353. With argument N, reinsert the Nth most recently killed stretch of killed
  1354. text.
  1355. See also the command \\[yank-pop]."
  1356.   (interactive "*P")
  1357.   ;; If we don't get all the way through, make last-command indicate that
  1358.   ;; for the following command.
  1359.   (setq this-command t)
  1360.   (push-mark (point))
  1361.   (insert (current-kill (cond
  1362.              ((listp arg) 0)
  1363.              ((eq arg '-) -1)
  1364.              (t (1- arg)))))
  1365.   (if (consp arg)
  1366.       ;; This is like exchange-point-and-mark, but doesn't activate the mark.
  1367.       ;; It is cleaner to avoid activation, even though the command
  1368.       ;; loop would deactivate the mark because we inserted text.
  1369.       ;; (But it's an unnecessary kludge in XEmacs.)
  1370.       ;(goto-char (prog1 (mark t)
  1371.            ;(set-marker (mark-marker) (point) (current-buffer)))))
  1372.       (exchange-point-and-mark t))
  1373.   ;; If we do get all the way thru, make this-command indicate that.
  1374.   (setq this-command 'yank)
  1375.   nil)
  1376.  
  1377. (defun rotate-yank-pointer (arg)
  1378.   "Rotate the yanking point in the kill ring.
  1379. With argument, rotate that many kills forward (or backward, if negative)."
  1380.   (interactive "p")
  1381.   (current-kill arg))
  1382.  
  1383.  
  1384. (defun insert-buffer (buffer)
  1385.   "Insert after point the contents of BUFFER.
  1386. Puts mark after the inserted text.
  1387. BUFFER may be a buffer or a buffer name."
  1388.   (interactive
  1389.    (list
  1390.     (progn
  1391.       (barf-if-buffer-read-only)
  1392.       (read-buffer "Insert buffer: " 
  1393.            ;; XEmacs: we have different args
  1394.            (other-buffer (current-buffer) nil t)
  1395.            t))))
  1396.   (or (bufferp buffer)
  1397.       (setq buffer (get-buffer buffer)))
  1398.   (let (start end newmark)
  1399.     (save-excursion
  1400.       (save-excursion
  1401.     (set-buffer buffer)
  1402.     (setq start (point-min) end (point-max)))
  1403.       (insert-buffer-substring buffer start end)
  1404.       (setq newmark (point)))
  1405.     (push-mark newmark))
  1406.   nil)
  1407.  
  1408. (defun append-to-buffer (buffer start end)
  1409.   "Append to specified buffer the text of the region.
  1410. It is inserted into that buffer before its point.
  1411.  
  1412. When calling from a program, give three arguments:
  1413. BUFFER (or buffer name), START and END.
  1414. START and END specify the portion of the current buffer to be copied."
  1415.   (interactive
  1416.    ;; XEmacs: we have different args to other-buffer
  1417.    (list (read-buffer "Append to buffer: " (other-buffer (current-buffer)
  1418.                              nil t))
  1419.      (region-beginning) (region-end)))
  1420.   (let ((oldbuf (current-buffer)))
  1421.     (save-excursion
  1422.       (set-buffer (get-buffer-create buffer))
  1423.       (insert-buffer-substring oldbuf start end))))
  1424.  
  1425. (defun prepend-to-buffer (buffer start end)
  1426.   "Prepend to specified buffer the text of the region.
  1427. It is inserted into that buffer after its point.
  1428.  
  1429. When calling from a program, give three arguments:
  1430. BUFFER (or buffer name), START and END.
  1431. START and END specify the portion of the current buffer to be copied."
  1432.   (interactive "BPrepend to buffer: \nr")
  1433.   (let ((oldbuf (current-buffer)))
  1434.     (save-excursion
  1435.       (set-buffer (get-buffer-create buffer))
  1436.       (save-excursion
  1437.     (insert-buffer-substring oldbuf start end)))))
  1438.  
  1439. (defun copy-to-buffer (buffer start end)
  1440.   "Copy to specified buffer the text of the region.
  1441. It is inserted into that buffer, replacing existing text there.
  1442.  
  1443. When calling from a program, give three arguments:
  1444. BUFFER (or buffer name), START and END.
  1445. START and END specify the portion of the current buffer to be copied."
  1446.   (interactive "BCopy to buffer: \nr")
  1447.   (let ((oldbuf (current-buffer)))
  1448.     (save-excursion
  1449.       (set-buffer (get-buffer-create buffer))
  1450.       (erase-buffer)
  1451.       (save-excursion
  1452.     (insert-buffer-substring oldbuf start end)))))
  1453.  
  1454. ;FSFmacs
  1455. ;(put 'mark-inactive 'error-conditions '(mark-inactive error))
  1456. ;(put 'mark-inactive 'error-message "The mark is not active now")
  1457.  
  1458. (defun mark (&optional force buffer)
  1459.   "Return this buffer's mark value as integer, or nil if no mark.
  1460.  
  1461. If `zmacs-regions' is true, then this returns nil unless the region is
  1462. currently in the active (highlighted) state.  With an argument of t, this
  1463. returns the mark (if there is one) regardless of the active-region state.
  1464. You should *generally* not use the mark unless the region is active, if
  1465. the user has expressed a preference for the active-region model.
  1466.  
  1467. If you are using this in an editing command, you are most likely making
  1468. a mistake; see the documentation of `set-mark'."
  1469.   (setq buffer (decode-buffer buffer))
  1470. ;FSFmacs version:
  1471. ;  (if (or force (not transient-mark-mode) mark-active mark-even-if-inactive)
  1472. ;      (marker-position (mark-marker))
  1473. ;    (signal 'mark-inactive nil)))
  1474.   (let ((m (mark-marker force buffer)))
  1475.     (and m (marker-position m))))
  1476.  
  1477. ;;;#### FSFmacs
  1478. ;;; Many places set mark-active directly, and several of them failed to also
  1479. ;;; run deactivate-mark-hook.  This shorthand should simplify.
  1480. ;(defsubst deactivate-mark ()
  1481. ;  "Deactivate the mark by setting `mark-active' to nil.
  1482. ;\(That makes a difference only in Transient Mark mode.)
  1483. ;Also runs the hook `deactivate-mark-hook'."
  1484. ;  (if transient-mark-mode
  1485. ;      (progn
  1486. ;    (setq mark-active nil)
  1487. ;    (run-hooks 'deactivate-mark-hook))))
  1488.  
  1489. (defun set-mark (pos &optional buffer)
  1490.   "Set this buffer's mark to POS.  Don't use this function!
  1491. That is to say, don't use this function unless you want
  1492. the user to see that the mark has moved, and you want the previous
  1493. mark position to be lost.
  1494.  
  1495. Normally, when a new mark is set, the old one should go on the stack.
  1496. This is why most applications should use push-mark, not set-mark.
  1497.  
  1498. Novice Emacs Lisp programmers often try to use the mark for the wrong
  1499. purposes.  The mark saves a location for the user's convenience.
  1500. Most editing commands should not alter the mark.
  1501. To remember a location for internal use in the Lisp program,
  1502. store it in a Lisp variable.  Example:
  1503.  
  1504.    (let ((beg (point))) (forward-line 1) (delete-region beg (point)))."
  1505.  
  1506.   (setq buffer (decode-buffer buffer))
  1507.   (set-marker (mark-marker t buffer) pos buffer))
  1508. ;; FSF
  1509. ;  (if pos
  1510. ;     (progn
  1511. ;    (setq mark-active t)
  1512. ;    (run-hooks 'activate-mark-hook)
  1513. ;    (set-marker (mark-marker) pos (current-buffer)))
  1514. ;    ;; Normally we never clear mark-active except in Transient Mark mode.
  1515. ;    ;; But when we actually clear out the mark value too,
  1516. ;    ;; we must clear mark-active in any mode.
  1517. ;    (setq mark-active nil)
  1518. ;    (run-hooks 'deactivate-mark-hook)
  1519. ;    (set-marker (mark-marker) nil)))
  1520.  
  1521. (defvar mark-ring nil
  1522.   "The list of former marks of the current buffer, most recent first.")
  1523. (make-variable-buffer-local 'mark-ring)
  1524. (put 'mark-ring 'permanent-local t)
  1525.  
  1526. (defcustom mark-ring-max 16
  1527.   "*Maximum size of mark ring.  Start discarding off end if gets this big."
  1528.   :type 'integer
  1529.   :group 'killing)
  1530.  
  1531. (defvar global-mark-ring nil
  1532.   "The list of saved global marks, most recent first.")
  1533.  
  1534. (defcustom global-mark-ring-max 16
  1535.   "*Maximum size of global mark ring.  \
  1536. Start discarding off end if gets this big."
  1537.   :type 'integer
  1538.   :group 'killing)
  1539.  
  1540. (defun set-mark-command (arg)
  1541.   "Set mark at where point is, or jump to mark.
  1542. With no prefix argument, set mark, push old mark position on local mark
  1543. ring, and push mark on global mark ring.
  1544. With argument, jump to mark, and pop a new position for mark off the ring
  1545. \(does not affect global mark ring\).
  1546.  
  1547. Novice Emacs Lisp programmers often try to use the mark for the wrong
  1548. purposes.  See the documentation of `set-mark' for more information."
  1549.   (interactive "P")
  1550.   (if (null arg)
  1551.       (push-mark nil nil t)
  1552.     (if (null (mark t))
  1553.     (error "No mark set in this buffer")
  1554.       (goto-char (mark t))
  1555.       (pop-mark))))
  1556.  
  1557. ;; XEmacs: Extra parameter
  1558. (defun push-mark (&optional location nomsg activate-region buffer)
  1559.   "Set mark at LOCATION (point, by default) and push old mark on mark ring.
  1560. If the last global mark pushed was not in the current buffer,
  1561. also push LOCATION on the global mark ring.
  1562. Display `Mark set' unless the optional second arg NOMSG is non-nil.
  1563. Activate mark if optional third arg ACTIVATE-REGION non-nil.
  1564.  
  1565. Novice Emacs Lisp programmers often try to use the mark for the wrong
  1566. purposes.  See the documentation of `set-mark' for more information."
  1567.   (setq buffer (decode-buffer buffer)) ; XEmacs
  1568.   (if (null (mark t buffer)) ; XEmacs
  1569.       nil
  1570.     ;; The save-excursion / set-buffer is necessary because mark-ring
  1571.     ;; is a buffer local variable
  1572.     (save-excursion
  1573.       (set-buffer buffer)
  1574.       (setq mark-ring (cons (copy-marker (mark-marker t buffer)) mark-ring))
  1575.       (if (> (length mark-ring) mark-ring-max)
  1576.       (progn
  1577.         (move-marker (car (nthcdr mark-ring-max mark-ring)) nil buffer)
  1578.         (setcdr (nthcdr (1- mark-ring-max) mark-ring) nil)))))
  1579.   (set-mark (or location (point buffer)) buffer)
  1580. ; (set-marker (mark-marker) (or location (point)) (current-buffer)) ; FSF
  1581.   ;; Now push the mark on the global mark ring.
  1582.   (if (or (null global-mark-ring)
  1583.           (not (eq (marker-buffer (car global-mark-ring)) buffer)))
  1584.       ;; The last global mark pushed wasn't in this same buffer.
  1585.       (progn
  1586.         (setq global-mark-ring (cons (copy-marker (mark-marker t buffer))
  1587.                                      global-mark-ring))
  1588.         (if (> (length global-mark-ring) global-mark-ring-max)
  1589.             (progn
  1590.               (move-marker (car (nthcdr global-mark-ring-max global-mark-ring))
  1591.                            nil buffer)
  1592.               (setcdr (nthcdr (1- global-mark-ring-max) global-mark-ring) nil)))))
  1593.   (or nomsg executing-kbd-macro (> (minibuffer-depth) 0)
  1594.       (display-message 'command "Mark set"))
  1595.   (if activate-region
  1596.       (progn
  1597.     (setq zmacs-region-stays t)
  1598.     (zmacs-activate-region)))
  1599. ; (if (or activate (not transient-mark-mode)) ; FSF
  1600. ;     (set-mark (mark t))) ; FSF
  1601.   nil)
  1602.  
  1603. (defun pop-mark ()
  1604.   "Pop off mark ring into the buffer's actual mark.
  1605. Does not set point.  Does nothing if mark ring is empty."
  1606.   (if mark-ring
  1607.       (progn
  1608.     (setq mark-ring (nconc mark-ring (list (copy-marker (mark-marker t)))))
  1609.     (set-mark (car mark-ring))
  1610.     (move-marker (car mark-ring) nil)
  1611.     (if (null (mark t)) (ding))
  1612.     (setq mark-ring (cdr mark-ring)))))
  1613.  
  1614. (define-function 'exchange-dot-and-mark 'exchange-point-and-mark)
  1615. (defun exchange-point-and-mark (&optional dont-activate-region)
  1616.   "Put the mark where point is now, and point where the mark is now.
  1617. The mark is activated unless DONT-ACTIVATE-REGION is non-nil."
  1618.   (interactive nil)
  1619.   (let ((omark (mark t)))
  1620.     (if (null omark)
  1621.     (error "No mark set in this buffer"))
  1622.     (set-mark (point))
  1623.     (goto-char omark)
  1624.     (or dont-activate-region (zmacs-activate-region)) ; XEmacs
  1625.     nil))
  1626.  
  1627. ;; XEmacs
  1628. (defun mark-something (mark-fn movement-fn arg)
  1629.   "internal function used by mark-sexp, mark-word, etc."
  1630.   (let (newmark (pushp t))
  1631.     (save-excursion
  1632.       (if (and (eq last-command mark-fn) (mark))
  1633.       ;; Extend the previous state in the same direction:
  1634.       (progn
  1635.         (if (< (mark) (point)) (setq arg (- arg)))
  1636.         (goto-char (mark))
  1637.         (setq pushp nil)))
  1638.       (funcall movement-fn arg)
  1639.       (setq newmark (point)))
  1640.     (if pushp
  1641.     (push-mark newmark nil t)
  1642.       ;; Do not mess with the mark stack, but merely adjust the previous state:
  1643.       (set-mark newmark)
  1644.       (activate-region))))
  1645.  
  1646. ;(defun transient-mark-mode (arg)
  1647. ;  "Toggle Transient Mark mode.
  1648. ;With arg, turn Transient Mark mode on if arg is positive, off otherwise.
  1649. ;
  1650. ;In Transient Mark mode, when the mark is active, the region is highlighted.
  1651. ;Changing the buffer \"deactivates\" the mark.
  1652. ;So do certain other operations that set the mark
  1653. ;but whose main purpose is something else--for example,
  1654. ;incremental search, \\[beginning-of-buffer], and \\[end-of-buffer]."
  1655. ;  (interactive "P")
  1656. ;  (setq transient-mark-mode
  1657. ;    (if (null arg)
  1658. ;        (not transient-mark-mode)
  1659. ;      (> (prefix-numeric-value arg) 0))))
  1660.  
  1661. (defun pop-global-mark ()
  1662.   "Pop off global mark ring and jump to the top location."
  1663.   (interactive)
  1664.   ;; Pop entries which refer to non-existent buffers.
  1665.   (while (and global-mark-ring (not (marker-buffer (car global-mark-ring))))
  1666.     (setq global-mark-ring (cdr global-mark-ring)))
  1667.   (or global-mark-ring
  1668.       (error "No global mark set"))
  1669.   (let* ((marker (car global-mark-ring))
  1670.      (buffer (marker-buffer marker))
  1671.      (position (marker-position marker)))
  1672.     (setq global-mark-ring (nconc (cdr global-mark-ring)
  1673.                   (list (car global-mark-ring))))
  1674.     (set-buffer buffer)
  1675.     (or (and (>= position (point-min))
  1676.          (<= position (point-max)))
  1677.     (widen))
  1678.     (goto-char position)
  1679.     (switch-to-buffer buffer)))
  1680.  
  1681.  
  1682. ;;; After 8 years of waiting ... -sb
  1683. (defcustom next-line-add-newlines nil  ; XEmacs
  1684.   "*If non-nil, `next-line' inserts newline when the point is at end of buffer.
  1685. This behavior used to be the default, and is still default in FSF Emacs.
  1686. We think it is an unnecessary and unwanted side-effect."
  1687.   :type 'boolean
  1688.   :group 'editing-basics)
  1689.  
  1690. (defun next-line (arg)
  1691.   "Move cursor vertically down ARG lines.
  1692. If there is no character in the target line exactly under the current column,
  1693. the cursor is positioned after the character in that line which spans this
  1694. column, or at the end of the line if it is not long enough.
  1695.  
  1696. If there is no line in the buffer after this one, behavior depends on the
  1697. value of `next-line-add-newlines'.  If non-nil, it inserts a newline character
  1698. to create a line, and moves the cursor to that line.  Otherwise it moves the
  1699. cursor to the end of the buffer.
  1700.  
  1701. The command \\[set-goal-column] can be used to create
  1702. a semipermanent goal column to which this command always moves.
  1703. Then it does not try to move vertically.  This goal column is stored
  1704. in `goal-column', which is nil when there is none.
  1705.  
  1706. If you are thinking of using this in a Lisp program, consider
  1707. using `forward-line' instead.  It is usually easier to use
  1708. and more reliable (no dependence on goal column, etc.)."
  1709.   (interactive "_p") ; XEmacs
  1710.   (if (and next-line-add-newlines (= arg 1))
  1711.       (let ((opoint (point)))
  1712.     (end-of-line)
  1713.     (if (eobp)
  1714.         (newline 1)
  1715.       (goto-char opoint)
  1716.       (line-move arg)))
  1717.     (if (interactive-p)
  1718.     ;; XEmacs:  Not sure what to do about this.  It's inconsistent. -sb
  1719.     (condition-case nil
  1720.         (line-move arg)
  1721.       ((beginning-of-buffer end-of-buffer)
  1722.        (when signal-error-on-buffer-boundary
  1723.          (ding nil 'buffer-bound))))
  1724.       (line-move arg)))
  1725.   nil)
  1726.  
  1727. (defun previous-line (arg)
  1728.   "Move cursor vertically up ARG lines.
  1729. If there is no character in the target line exactly over the current column,
  1730. the cursor is positioned after the character in that line which spans this
  1731. column, or at the end of the line if it is not long enough.
  1732.  
  1733. The command \\[set-goal-column] can be used to create
  1734. a semipermanent goal column to which this command always moves.
  1735. Then it does not try to move vertically.
  1736.  
  1737. If you are thinking of using this in a Lisp program, consider using
  1738. `forward-line' with a negative argument instead.  It is usually easier
  1739. to use and more reliable (no dependence on goal column, etc.)."
  1740.   (interactive "_p") ; XEmacs
  1741.   (if (interactive-p)
  1742.       (condition-case nil
  1743.       (line-move (- arg))
  1744.     ((beginning-of-buffer end-of-buffer)
  1745.      (when signal-error-on-buffer-boundary ; XEmacs
  1746.        (ding nil 'buffer-bound))))
  1747.     (line-move (- arg)))
  1748.   nil)
  1749.  
  1750. (defcustom track-eol nil
  1751.   "*Non-nil means vertical motion starting at end of line keeps to ends of lines.
  1752. This means moving to the end of each line moved onto.
  1753. The beginning of a blank line does not count as the end of a line."
  1754.   :type 'boolean
  1755.   :group 'editing-basics)
  1756.  
  1757. (defcustom goal-column nil
  1758.   "*Semipermanent goal column for vertical motion, as set by \\[set-goal-column], or nil."
  1759.   :type '(choice integer (const :tag "None" nil))
  1760.   :group 'editing-basics)
  1761. (make-variable-buffer-local 'goal-column)
  1762.  
  1763. (defvar temporary-goal-column 0
  1764.   "Current goal column for vertical motion.
  1765. It is the column where point was
  1766. at the start of current run of vertical motion commands.
  1767. When the `track-eol' feature is doing its job, the value is 9999.")
  1768.  
  1769. ;XEmacs: not yet ported, so avoid compiler warnings
  1770. (eval-when-compile
  1771.   (defvar inhibit-point-motion-hooks))
  1772.  
  1773. (defcustom line-move-ignore-invisible nil
  1774.   "*Non-nil means \\[next-line] and \\[previous-line] ignore invisible lines.
  1775. Use with care, as it slows down movement significantly.  Outline mode sets this."
  1776.   :type 'boolean
  1777.   :group 'editing-basics)
  1778.  
  1779. ;; This is the guts of next-line and previous-line.
  1780. ;; Arg says how many lines to move.
  1781. (defun line-move (arg)
  1782.   ;; Don't run any point-motion hooks, and disregard intangibility,
  1783.   ;; for intermediate positions.
  1784.   (let ((inhibit-point-motion-hooks t)
  1785.     (opoint (point))
  1786.     new)
  1787.     (unwind-protect
  1788.     (progn
  1789.       (if (not (or (eq last-command 'next-line)
  1790.                (eq last-command 'previous-line)))
  1791.           (setq temporary-goal-column
  1792.             (if (and track-eol (eolp)
  1793.                  ;; Don't count beg of empty line as end of line
  1794.                  ;; unless we just did explicit end-of-line.
  1795.                  (or (not (bolp)) (eq last-command 'end-of-line)))
  1796.             9999
  1797.               (current-column))))
  1798.       (if (and (not (integerp selective-display))
  1799.            (not line-move-ignore-invisible))
  1800.           ;; Use just newline characters.
  1801.           (or (if (> arg 0)
  1802.               (progn (if (> arg 1) (forward-line (1- arg)))
  1803.                  ;; This way of moving forward ARG lines
  1804.                  ;; verifies that we have a newline after the last one.
  1805.                  ;; It doesn't get confused by intangible text.
  1806.                  (end-of-line)
  1807.                  (zerop (forward-line 1)))
  1808.             (and (zerop (forward-line arg))
  1809.              (bolp)))
  1810.           (signal (if (< arg 0)
  1811.                   'beginning-of-buffer
  1812.                 'end-of-buffer)
  1813.               nil))
  1814.         ;; Move by arg lines, but ignore invisible ones.
  1815.         (while (> arg 0)
  1816.           (end-of-line)
  1817.           (and (zerop (vertical-motion 1))
  1818.            (signal 'end-of-buffer nil))
  1819.           ;; If the following character is currently invisible,
  1820.           ;; skip all characters with that same `invisible' property value.
  1821.           (while (and (not (eobp))
  1822.               (let ((prop
  1823.                  (get-char-property (point) 'invisible)))
  1824.                 (if (eq buffer-invisibility-spec t)
  1825.                 prop
  1826.                   (or (memq prop buffer-invisibility-spec)
  1827.                   (assq prop buffer-invisibility-spec)))))
  1828.         (if (get-text-property (point) 'invisible)
  1829.             (goto-char (next-single-property-change (point) 'invisible))
  1830.           (goto-char (next-extent-change (point))))) ; XEmacs
  1831.           (setq arg (1- arg)))
  1832.         (while (< arg 0)
  1833.           (beginning-of-line)
  1834.           (and (zerop (vertical-motion -1))
  1835.            (signal 'beginning-of-buffer nil))
  1836.           (while (and (not (bobp))
  1837.               (let ((prop
  1838.                  (get-char-property (1- (point)) 'invisible)))
  1839.                 (if (eq buffer-invisibility-spec t)
  1840.                 prop
  1841.                   (or (memq prop buffer-invisibility-spec)
  1842.                   (assq prop buffer-invisibility-spec)))))
  1843.         (if (get-text-property (1- (point)) 'invisible)
  1844.             (goto-char (previous-single-property-change (point) 'invisible))
  1845.           (goto-char (previous-extent-change (point))))) ; XEmacs
  1846.           (setq arg (1+ arg))))
  1847.       (move-to-column (or goal-column temporary-goal-column)))
  1848.       ;; Remember where we moved to, go back home,
  1849.       ;; then do the motion over again
  1850.       ;; in just one step, with intangibility and point-motion hooks
  1851.       ;; enabled this time.
  1852.       (setq new (point))
  1853.       (goto-char opoint)
  1854.       (setq inhibit-point-motion-hooks nil)
  1855.       (goto-char new)))
  1856.   nil)
  1857.  
  1858. ;;; Many people have said they rarely use this feature, and often type
  1859. ;;; it by accident.  Maybe it shouldn't even be on a key.
  1860. ;; It's not on a key, as of 20.2.  So no need for this.
  1861. ;(put 'set-goal-column 'disabled t)
  1862.  
  1863. (defun set-goal-column (arg)
  1864.   "Set the current horizontal position as a goal for \\[next-line] and \\[previous-line].
  1865. Those commands will move to this position in the line moved to
  1866. rather than trying to keep the same horizontal position.
  1867. With a non-nil argument, clears out the goal column
  1868. so that \\[next-line] and \\[previous-line] resume vertical motion.
  1869. The goal column is stored in the variable `goal-column'."
  1870.   (interactive "_P") ; XEmacs
  1871.   (if arg
  1872.       (progn
  1873.         (setq goal-column nil)
  1874.         (display-message 'command "No goal column"))
  1875.     (setq goal-column (current-column))
  1876.     (message (substitute-command-keys
  1877.           "Goal column %d (use \\[set-goal-column] with an arg to unset it)")
  1878.          goal-column))
  1879.   nil)
  1880.  
  1881. ;; deleted FSFmacs terminal randomness hscroll-point-visible stuff.
  1882. ;; hscroll-step
  1883. ;; hscroll-point-visible
  1884. ;; hscroll-window-column
  1885. ;; right-arrow
  1886. ;; left-arrow
  1887.  
  1888. (defun scroll-other-window-down (lines)
  1889.   "Scroll the \"other window\" down.
  1890. For more details, see the documentation for `scroll-other-window'."
  1891.   (interactive "P")
  1892.   (scroll-other-window
  1893.    ;; Just invert the argument's meaning.
  1894.    ;; We can do that without knowing which window it will be.
  1895.    (if (eq lines '-) nil
  1896.      (if (null lines) '-
  1897.        (- (prefix-numeric-value lines))))))
  1898. ;(define-key esc-map [?\C-\S-v] 'scroll-other-window-down)
  1899.  
  1900. (defun beginning-of-buffer-other-window (arg)
  1901.   "Move point to the beginning of the buffer in the other window.
  1902. Leave mark at previous position.
  1903. With arg N, put point N/10 of the way from the true beginning."
  1904.   (interactive "P")
  1905.   (let ((orig-window (selected-window))
  1906.     (window (other-window-for-scrolling)))
  1907.     ;; We use unwind-protect rather than save-window-excursion
  1908.     ;; because the latter would preserve the things we want to change.
  1909.     (unwind-protect
  1910.     (progn
  1911.       (select-window window)
  1912.       ;; Set point and mark in that window's buffer.
  1913.       (beginning-of-buffer arg)
  1914.       ;; Set point accordingly.
  1915.       (recenter '(t)))
  1916.       (select-window orig-window))))
  1917.  
  1918. (defun end-of-buffer-other-window (arg)
  1919.   "Move point to the end of the buffer in the other window.
  1920. Leave mark at previous position.
  1921. With arg N, put point N/10 of the way from the true end."
  1922.   (interactive "P")
  1923.   ;; See beginning-of-buffer-other-window for comments.
  1924.   (let ((orig-window (selected-window))
  1925.     (window (other-window-for-scrolling)))
  1926.     (unwind-protect
  1927.     (progn
  1928.       (select-window window)
  1929.       (end-of-buffer arg)
  1930.       (recenter '(t)))
  1931.       (select-window orig-window))))
  1932.  
  1933. (defun transpose-chars (arg)
  1934.   "Interchange characters around point, moving forward one character.
  1935. With prefix arg ARG, effect is to take character before point
  1936. and drag it forward past ARG other characters (backward if ARG negative).
  1937. If no argument and at end of line, the previous two chars are exchanged."
  1938.   (interactive "*P")
  1939.   (and (null arg) (eolp) (forward-char -1))
  1940.   (transpose-subr 'forward-char (prefix-numeric-value arg)))
  1941.  
  1942. ;;; A very old implementation of transpose-chars from the old days ...
  1943. (defun transpose-preceding-chars (arg)
  1944.   "Interchange characters before point.
  1945. With prefix arg ARG, effect is to take character before point
  1946. and drag it forward past ARG other characters (backward if ARG negative).
  1947. If no argument and not at start of line, the previous two chars are exchanged."
  1948.   (interactive "*P")
  1949.   (and (null arg) (not (bolp)) (forward-char -1))
  1950.   (transpose-subr 'forward-char (prefix-numeric-value arg)))
  1951.  
  1952.  
  1953. (defun transpose-words (arg)
  1954.   "Interchange words around point, leaving point at end of them.
  1955. With prefix arg ARG, effect is to take word before or around point
  1956. and drag it forward past ARG other words (backward if ARG negative).
  1957. If ARG is zero, the words around or after point and around or after mark
  1958. are interchanged."
  1959.   (interactive "*p")
  1960.   (transpose-subr 'forward-word arg))
  1961.  
  1962. (defun transpose-sexps (arg)
  1963.   "Like \\[transpose-words] but applies to sexps.
  1964. Does not work on a sexp that point is in the middle of
  1965. if it is a list or string."
  1966.   (interactive "*p")
  1967.   (transpose-subr 'forward-sexp arg))
  1968.  
  1969. (defun transpose-lines (arg)
  1970.   "Exchange current line and previous line, leaving point after both.
  1971. With argument ARG, takes previous line and moves it past ARG lines.
  1972. With argument 0, interchanges line point is in with line mark is in."
  1973.   (interactive "*p")
  1974.   (transpose-subr #'(lambda (arg)
  1975.              (if (= arg 1)
  1976.              (progn
  1977.                ;; Move forward over a line,
  1978.                ;; but create a newline if none exists yet.
  1979.                (end-of-line)
  1980.                (if (eobp)
  1981.                    (newline)
  1982.                  (forward-char 1)))
  1983.                (forward-line arg)))
  1984.           arg))
  1985.  
  1986. (eval-when-compile
  1987.   ;; avoid byte-compiler warnings...
  1988.   (defvar start1)
  1989.   (defvar start2)
  1990.   (defvar end1)
  1991.   (defvar end2))
  1992.  
  1993. ; start[12] and end[12] used in transpose-subr-1 below
  1994. (defun transpose-subr (mover arg)
  1995.   (let (start1 end1 start2 end2)
  1996.     (if (= arg 0)
  1997.     (progn
  1998.       (save-excursion
  1999.         (funcall mover 1)
  2000.         (setq end2 (point))
  2001.         (funcall mover -1)
  2002.         (setq start2 (point))
  2003.         (goto-char (mark t)) ; XEmacs
  2004.         (funcall mover 1)
  2005.         (setq end1 (point))
  2006.         (funcall mover -1)
  2007.         (setq start1 (point))
  2008.         (transpose-subr-1))
  2009.       (exchange-point-and-mark t))) ; XEmacs
  2010.     (while (> arg 0)
  2011.       (funcall mover -1)
  2012.       (setq start1 (point))
  2013.       (funcall mover 1)
  2014.       (setq end1 (point))
  2015.       (funcall mover 1)
  2016.       (setq end2 (point))
  2017.       (funcall mover -1)
  2018.       (setq start2 (point))
  2019.       (transpose-subr-1)
  2020.       (goto-char end2)
  2021.       (setq arg (1- arg)))
  2022.     (while (< arg 0)
  2023.       (funcall mover -1)
  2024.       (setq start2 (point))
  2025.       (funcall mover -1)
  2026.       (setq start1 (point))
  2027.       (funcall mover 1)
  2028.       (setq end1 (point))
  2029.       (funcall mover 1)
  2030.       (setq end2 (point))
  2031.       (transpose-subr-1)
  2032.       (setq arg (1+ arg)))))
  2033.  
  2034. ; start[12] and end[12] used free
  2035. (defun transpose-subr-1 ()
  2036.   (if (> (min end1 end2) (max start1 start2))
  2037.       (error "Don't have two things to transpose"))
  2038.   (let ((word1 (buffer-substring start1 end1))
  2039.     (word2 (buffer-substring start2 end2)))
  2040.     (delete-region start2 end2)
  2041.     (goto-char start2)
  2042.     (insert word1)
  2043.     (goto-char (if (< start1 start2) start1
  2044.          (+ start1 (- (length word1) (length word2)))))
  2045.     (delete-char (length word1))
  2046.     (insert word2)))
  2047.  
  2048. (defcustom comment-column 32
  2049.   "*Column to indent right-margin comments to.
  2050. Setting this variable automatically makes it local to the current buffer.
  2051. Each mode establishes a different default value for this variable; you
  2052. can set the value for a particular mode using that mode's hook."
  2053.   :type 'integer
  2054.   :group 'fill-comments)
  2055. (make-variable-buffer-local 'comment-column)
  2056.  
  2057. (defcustom comment-start nil
  2058.   "*String to insert to start a new comment, or nil if no comment syntax."
  2059.   :type '(choice (const :tag "None" nil)
  2060.          string)
  2061.   :group 'fill-comments)
  2062.  
  2063. (defcustom comment-start-skip nil
  2064.   "*Regexp to match the start of a comment plus everything up to its body.
  2065. If there are any \\(...\\) pairs, the comment delimiter text is held to begin
  2066. at the place matched by the close of the first pair."
  2067.   :type '(choice (const :tag "None" nil)
  2068.          regexp)
  2069.   :group 'fill-comments)
  2070.  
  2071. (defcustom comment-end ""
  2072.   "*String to insert to end a new comment.
  2073. Should be an empty string if comments are terminated by end-of-line."
  2074.   :type 'string
  2075.   :group 'fill-comments)
  2076.  
  2077. (defconst comment-indent-hook nil
  2078.   "Obsolete variable for function to compute desired indentation for a comment.
  2079. Use `comment-indent-function' instead.
  2080. This function is called with no args with point at the beginning of
  2081. the comment's starting delimiter.")
  2082.  
  2083. (defconst comment-indent-function
  2084.   ;; XEmacs - add at least one space after the end of the text on the
  2085.   ;; current line...  
  2086.   (lambda ()
  2087.     (save-excursion 
  2088.       (beginning-of-line) 
  2089.       (let ((eol (save-excursion (end-of-line) (point))))
  2090.     (and comment-start-skip
  2091.          (re-search-forward comment-start-skip eol t)
  2092.          (setq eol (match-beginning 0)))
  2093.     (goto-char eol)
  2094.     (skip-chars-backward " \t")
  2095.     (max comment-column (1+ (current-column))))))
  2096.   "Function to compute desired indentation for a comment.
  2097. This function is called with no args with point at the beginning of
  2098. the comment's starting delimiter.")
  2099.  
  2100. (defcustom block-comment-start nil
  2101.   "*String to insert to start a new comment on a line by itself.
  2102. If nil, use `comment-start' instead.
  2103. Note that the regular expression `comment-start-skip' should skip this string
  2104. as well as the `comment-start' string."
  2105.   :type '(choice (const :tag "Use `comment-start'" nil)
  2106.          string)
  2107.   :group 'fill-comments)
  2108.  
  2109. (defcustom block-comment-end nil
  2110.   "*String to insert to end a new comment on a line by itself.
  2111. Should be an empty string if comments are terminated by end-of-line.
  2112. If nil, use `comment-end' instead."
  2113.   :type '(choice (const :tag "Use `comment-end'" nil)
  2114.          string)
  2115.   :group 'fill-comments)
  2116.  
  2117. (defun indent-for-comment ()
  2118.   "Indent this line's comment to comment column, or insert an empty comment."
  2119.   (interactive "*")
  2120.   (let* ((empty (save-excursion (beginning-of-line)
  2121.                 (looking-at "[ \t]*$")))
  2122.      (starter (or (and empty block-comment-start) comment-start))
  2123.      (ender (or (and empty block-comment-end) comment-end)))
  2124.     (if (null starter)
  2125.     (error "No comment syntax defined")
  2126.       (let* ((eolpos (save-excursion (end-of-line) (point)))
  2127.          cpos indent begpos)
  2128.     (beginning-of-line)
  2129.     (if (re-search-forward comment-start-skip eolpos 'move)
  2130.         (progn (setq cpos (point-marker))
  2131.            ;; Find the start of the comment delimiter.
  2132.            ;; If there were paren-pairs in comment-start-skip,
  2133.            ;; position at the end of the first pair.
  2134.            (if (match-end 1)
  2135.                (goto-char (match-end 1))
  2136.              ;; If comment-start-skip matched a string with
  2137.              ;; internal whitespace (not final whitespace) then
  2138.              ;; the delimiter start at the end of that
  2139.              ;; whitespace.  Otherwise, it starts at the
  2140.              ;; beginning of what was matched.
  2141.              (skip-syntax-backward " " (match-beginning 0))
  2142.              (skip-syntax-backward "^ " (match-beginning 0)))))
  2143.     (setq begpos (point))
  2144.     ;; Compute desired indent.
  2145.     (if (= (current-column)
  2146.            (setq indent (funcall comment-indent-function)))
  2147.         (goto-char begpos)
  2148.       ;; If that's different from current, change it.
  2149.       (skip-chars-backward " \t")
  2150.       (delete-region (point) begpos)
  2151.       (indent-to indent))
  2152.     ;; An existing comment?
  2153.     (if cpos 
  2154.         (progn (goto-char cpos)
  2155.            (set-marker cpos nil))
  2156.       ;; No, insert one.
  2157.       (insert starter)
  2158.       (save-excursion
  2159.         (insert ender)))))))
  2160.  
  2161. (defun set-comment-column (arg)
  2162.   "Set the comment column based on point.
  2163. With no arg, set the comment column to the current column.
  2164. With just minus as arg, kill any comment on this line.
  2165. With any other arg, set comment column to indentation of the previous comment
  2166.  and then align or create a comment on this line at that column."
  2167.   (interactive "P")
  2168.   (if (eq arg '-)
  2169.       (kill-comment nil)
  2170.     (if arg
  2171.     (progn
  2172.       (save-excursion
  2173.         (beginning-of-line)
  2174.         (re-search-backward comment-start-skip)
  2175.         (beginning-of-line)
  2176.         (re-search-forward comment-start-skip)
  2177.         (goto-char (match-beginning 0))
  2178.         (setq comment-column (current-column))
  2179.         (display-message
  2180.          'command
  2181.          (format "Comment column set to %d" comment-column)))
  2182.       (indent-for-comment))
  2183.       (setq comment-column (current-column))
  2184.       (display-message
  2185.        'command
  2186.        (format "Comment column set to %d" comment-column)))))
  2187.  
  2188. (defun kill-comment (arg)
  2189.   "Kill the comment on this line, if any.
  2190. With argument, kill comments on that many lines starting with this one."
  2191.   ;; this function loses in a lot of situations.  it incorrectly recognises
  2192.   ;; comment delimiters sometimes (ergo, inside a string), doesn't work
  2193.   ;; with multi-line comments, can kill extra whitespace if comment wasn't
  2194.   ;; through end-of-line, et cetera.
  2195.   (interactive "*P")
  2196.   (or comment-start-skip (error "No comment syntax defined"))
  2197.   (let ((count (prefix-numeric-value arg)) endc)
  2198.     (while (> count 0)
  2199.       (save-excursion
  2200.     (end-of-line)
  2201.     (setq endc (point))
  2202.     (beginning-of-line)
  2203.     (and (string< "" comment-end)
  2204.          (setq endc
  2205.            (progn
  2206.              (re-search-forward (regexp-quote comment-end) endc 'move)
  2207.              (skip-chars-forward " \t")
  2208.              (point))))
  2209.     (beginning-of-line)
  2210.     (if (re-search-forward comment-start-skip endc t)
  2211.         (progn
  2212.           (goto-char (match-beginning 0))
  2213.           (skip-chars-backward " \t")
  2214.           (kill-region (point) endc)
  2215.           ;; to catch comments a line beginnings
  2216.           (indent-according-to-mode))))
  2217.       (if arg (forward-line 1))
  2218.       (setq count (1- count)))))
  2219.  
  2220. (defun comment-region (beg end &optional arg)
  2221.   "Comment or uncomment each line in the region.
  2222. With just C-u prefix arg, uncomment each line in region.
  2223. Numeric prefix arg ARG means use ARG comment characters.
  2224. If ARG is negative, delete that many comment characters instead.
  2225. Comments are terminated on each line, even for syntax in which newline does
  2226. not end the comment.  Blank lines do not get comments."
  2227.   ;; if someone wants it to only put a comment-start at the beginning and
  2228.   ;; comment-end at the end then typing it, C-x C-x, closing it, C-x C-x
  2229.   ;; is easy enough.  No option is made here for other than commenting
  2230.   ;; every line.
  2231.   (interactive "r\nP")
  2232.   (or comment-start (error "No comment syntax is defined"))
  2233.   (if (> beg end) (let (mid) (setq mid beg beg end end mid)))
  2234.   (save-excursion
  2235.     (save-restriction
  2236.       (let ((cs comment-start) (ce comment-end)
  2237.         numarg)
  2238.         (if (consp arg) (setq numarg t)
  2239.       (setq numarg (prefix-numeric-value arg))
  2240.       ;; For positive arg > 1, replicate the comment delims now,
  2241.       ;; then insert the replicated strings just once.
  2242.       (while (> numarg 1)
  2243.         (setq cs (concat cs comment-start)
  2244.           ce (concat ce comment-end))
  2245.         (setq numarg (1- numarg))))
  2246.     ;; Loop over all lines from BEG to END.
  2247.         (narrow-to-region beg end)
  2248.         (goto-char beg)
  2249.         (while (not (eobp))
  2250.           (if (or (eq numarg t) (< numarg 0))
  2251.           (progn
  2252.         ;; Delete comment start from beginning of line.
  2253.         (if (eq numarg t)
  2254.             (while (looking-at (regexp-quote cs))
  2255.               (delete-char (length cs)))
  2256.           (let ((count numarg))
  2257.             (while (and (> 1 (setq count (1+ count)))
  2258.                 (looking-at (regexp-quote cs)))
  2259.               (delete-char (length cs)))))
  2260.         ;; Delete comment end from end of line.
  2261.                 (if (string= "" ce)
  2262.             nil
  2263.           (if (eq numarg t)
  2264.               (progn
  2265.             (end-of-line)
  2266.             ;; This is questionable if comment-end ends in
  2267.             ;; whitespace.  That is pretty brain-damaged,
  2268.             ;; though.
  2269.             (skip-chars-backward " \t")
  2270.             (if (and (>= (- (point) (point-min)) (length ce))
  2271.                  (save-excursion
  2272.                    (backward-char (length ce))
  2273.                    (looking-at (regexp-quote ce))))
  2274.                 (delete-char (- (length ce)))))
  2275.             (let ((count numarg))
  2276.               (while (> 1 (setq count (1+ count)))
  2277.             (end-of-line)
  2278.             ;; This is questionable if comment-end ends in
  2279.             ;; whitespace.  That is pretty brain-damaged though
  2280.             (skip-chars-backward " \t")
  2281.             (save-excursion
  2282.               (backward-char (length ce))
  2283.               (if (looking-at (regexp-quote ce))
  2284.                   (delete-char (length ce))))))))
  2285.         (forward-line 1))
  2286.         ;; Insert at beginning and at end.
  2287.             (if (looking-at "[ \t]*$") ()
  2288.               (insert cs)
  2289.               (if (string= "" ce) ()
  2290.                 (end-of-line)
  2291.                 (insert ce)))
  2292.             (search-forward "\n" nil 'move)))))))
  2293.  
  2294. ;; XEmacs
  2295. (defun prefix-region (prefix)
  2296.   "Add a prefix string to each line between mark and point."
  2297.   (interactive "sPrefix string: ")
  2298.   (if prefix
  2299.       (let ((count (count-lines (mark) (point))))
  2300.      (goto-char (min (mark) (point)))
  2301.      (while (> count 0)
  2302.           (setq count (1- count))
  2303.        (beginning-of-line 1)
  2304.        (insert prefix)
  2305.        (end-of-line 1)
  2306.        (forward-char 1)))))
  2307.  
  2308.  
  2309. ;; XEmacs - extra parameter
  2310. (defun backward-word (arg &optional buffer)
  2311.   "Move backward until encountering the end of a word.
  2312. With argument, do this that many times.
  2313. In programs, it is faster to call `forward-word' with negative arg."
  2314.   (interactive "_p") ; XEmacs
  2315.   (forward-word (- arg) buffer))
  2316.  
  2317. (defun mark-word (arg)
  2318.   "Set mark arg words away from point."
  2319.   (interactive "p")
  2320.   (mark-something 'mark-word 'forward-word arg))
  2321.  
  2322. ;; XEmacs modified
  2323. (defun kill-word (arg)
  2324.   "Kill characters forward until encountering the end of a word.
  2325. With argument, do this that many times."
  2326.   (interactive "*p")
  2327.   (kill-region (point) (save-excursion (forward-word arg) (point))))
  2328.  
  2329. (defun backward-kill-word (arg)
  2330.   "Kill characters backward until encountering the end of a word.
  2331. With argument, do this that many times."
  2332.   (interactive "*p") ; XEmacs
  2333.   (kill-word (- arg)))
  2334.  
  2335. (defun current-word (&optional strict)
  2336.   "Return the word point is on (or a nearby word) as a string.
  2337. If optional arg STRICT is non-nil, return nil unless point is within
  2338. or adjacent to a word.
  2339. If point is not between two word-constituent characters, but immediately
  2340. follows one, move back first.
  2341. Otherwise, if point precedes a word constituent, move forward first.
  2342. Otherwise, move backwards until a word constituent is found and get that word;
  2343. if you a newlines is reached first, move forward instead."
  2344.   (save-excursion
  2345.     (let ((oldpoint (point)) (start (point)) (end (point)))
  2346.       (skip-syntax-backward "w_") (setq start (point))
  2347.       (goto-char oldpoint)
  2348.       (skip-syntax-forward "w_") (setq end (point))
  2349.       (if (and (eq start oldpoint) (eq end oldpoint))
  2350.       ;; Point is neither within nor adjacent to a word.
  2351.       (and (not strict)
  2352.                (progn
  2353.                  ;; Look for preceding word in same line.
  2354.                  (skip-syntax-backward "^w_"
  2355.                                        (save-excursion
  2356.                                          (beginning-of-line) (point)))
  2357.                  (if (bolp)
  2358.              ;; No preceding word in same line.
  2359.              ;; Look for following word in same line.
  2360.                      (progn
  2361.                        (skip-syntax-forward "^w_"
  2362.                         (save-excursion
  2363.                                               (end-of-line) (point)))
  2364.                        (setq start (point))
  2365.                        (skip-syntax-forward "w_")
  2366.                        (setq end (point)))
  2367.                      (setq end (point))
  2368.                      (skip-syntax-backward "w_")
  2369.                      (setq start (point)))
  2370.          (buffer-substring start end)))
  2371.           (buffer-substring start end)))))
  2372.  
  2373. (defcustom fill-prefix nil
  2374.   "*String for filling to insert at front of new line, or nil for none.
  2375. Setting this variable automatically makes it local to the current buffer."
  2376.   :type '(choice (const :tag "None" nil)
  2377.          string)
  2378.   :group 'fill)
  2379. (make-variable-buffer-local 'fill-prefix)
  2380.  
  2381. (defcustom auto-fill-inhibit-regexp nil
  2382.   "*Regexp to match lines which should not be auto-filled."
  2383.   :type '(choice (const :tag "None" nil)
  2384.          regexp)
  2385.   :group 'fill)
  2386.  
  2387. (defvar comment-line-break-function 'indent-new-comment-line
  2388.   "*Mode-specific function which line breaks and continues a comment.
  2389.  
  2390. This function is only called during auto-filling of a comment section.
  2391. The function should take a single optional argument which is a flag
  2392. indicating whether soft newlines should be inserted.")
  2393.  
  2394. ;; This function is the auto-fill-function of a buffer
  2395. ;; when Auto-Fill mode is enabled.
  2396. ;; It returns t if it really did any work.
  2397. ;; XEmacs:  This function is totally different.
  2398. (defun do-auto-fill ()
  2399.   (let (give-up)
  2400.     (or (and auto-fill-inhibit-regexp
  2401.          (save-excursion (beginning-of-line)
  2402.                  (looking-at auto-fill-inhibit-regexp)))
  2403.     (while (and (not give-up) (> (current-column) fill-column))
  2404.       ;; Determine where to split the line.
  2405.       (let ((fill-prefix fill-prefix)
  2406.         (fill-point
  2407.          (let ((opoint (point))
  2408.                bounce
  2409.                ;; 97/3/14 jhod: Kinsoku
  2410.                (re-break-point (if (featurep 'mule)
  2411.                         (concat "[ \t\n]\\|" word-across-newline)
  2412.                     "[ \t\n]"))
  2413.                ;; end patch
  2414.                (first t))
  2415.            (save-excursion
  2416.              (move-to-column (1+ fill-column))
  2417.              ;; Move back to a word boundary.
  2418.              (while (or first
  2419.                 ;; If this is after period and a single space,
  2420.                 ;; move back once more--we don't want to break
  2421.                 ;; the line there and make it look like a
  2422.                 ;; sentence end.
  2423.                 (and (not (bobp))
  2424.                      (not bounce)
  2425.                      sentence-end-double-space
  2426.                      (save-excursion (forward-char -1)
  2427.                              (and (looking-at "\\. ")
  2428.                               (not (looking-at "\\.  "))))))
  2429.                (setq first nil)
  2430.                ;; 97/3/14 jhod: Kinsoku
  2431.                ; (skip-chars-backward "^ \t\n"))
  2432.                (fill-move-backward-to-break-point re-break-point)
  2433.                ;; end patch
  2434.                ;; If we find nowhere on the line to break it,
  2435.                ;; break after one word.  Set bounce to t
  2436.                ;; so we will not keep going in this while loop.
  2437.                (if (bolp)
  2438.                (progn
  2439.                  ;; 97/3/14 jhod: Kinsoku
  2440.                  ; (re-search-forward "[ \t]" opoint t)
  2441.                  (fill-move-forward-to-break-point re-break-point
  2442.                                    opoint)
  2443.                  ;; end patch
  2444.                  (setq bounce t)))
  2445.                (skip-chars-backward " \t"))
  2446.              (if (and (featurep 'mule)
  2447.                   (or bounce (bolp))) (kinsoku-process)) ;; 97/3/14 jhod: Kinsoku
  2448.              ;; Let fill-point be set to the place where we end up.
  2449.              (point)))))
  2450.  
  2451.         ;; I'm not sure why Stig made this change but it breaks
  2452.         ;; auto filling in at least C mode so I'm taking it back
  2453.         ;; out.  --cet
  2454.         ;; XEmacs - adaptive fill.
  2455.         ;;(maybe-adapt-fill-prefix
  2456.         ;; (or from (setq from (save-excursion (beginning-of-line)
  2457.         ;;                     (point))))
  2458.         ;; (or to   (setq to (save-excursion (beginning-of-line 2)
  2459.         ;;                       (point))))
  2460.         ;; t)
  2461.  
  2462.         ;; If that place is not the beginning of the line,
  2463.         ;; break the line there.
  2464.         (if (save-excursion
  2465.           (goto-char fill-point)
  2466.           (not (or (bolp) (eolp)))) ; 97/3/14 jhod: during kinsoku processing it is possible to move beyond
  2467.         (let ((prev-column (current-column)))
  2468.           ;; If point is at the fill-point, do not `save-excursion'.
  2469.           ;; Otherwise, if a comment prefix or fill-prefix is inserted,
  2470.           ;; point will end up before it rather than after it.
  2471.           (if (save-excursion
  2472.             (skip-chars-backward " \t")
  2473.             (= (point) fill-point))
  2474.               ;; 97/3/14 jhod: Kinsoku processing
  2475.               ;(indent-new-comment-line)
  2476.               (let ((spacep (memq (char-before (point)) '(?\  ?\t))))
  2477.             (funcall comment-line-break-function)
  2478.             ;; if user type space explicitly, leave SPC
  2479.             ;; even if there is no WAN.
  2480.             (if spacep
  2481.                 (save-excursion
  2482.                   (goto-char fill-point)
  2483.                   ;; put SPC except that there is SPC
  2484.                   ;; already or there is sentence end.
  2485.                   (or (memq (char-after (point)) '(?\  ?\t))
  2486.                   (fill-end-of-sentence-p)
  2487.                   (insert ?\ )))))
  2488.             (save-excursion
  2489.               (goto-char fill-point)
  2490.               (funcall comment-line-break-function)))
  2491.           ;; If making the new line didn't reduce the hpos of
  2492.           ;; the end of the line, then give up now;
  2493.           ;; trying again will not help.
  2494.           (if (>= (current-column) prev-column)
  2495.               (setq give-up t)))
  2496.           ;; No place to break => stop trying.
  2497.           (setq give-up t)))))))
  2498.  
  2499. ;; Put FSF one in until I can one or the other working properly, then the
  2500. ;; other one is history.
  2501. (defun fsf:do-auto-fill ()
  2502.   (let (fc justify
  2503.        ;; bol
  2504.        give-up
  2505.        (fill-prefix fill-prefix))
  2506.     (if (or (not (setq justify (current-justification)))
  2507.         (null (setq fc (current-fill-column)))
  2508.         (and (eq justify 'left)
  2509.          (<= (current-column) fc))
  2510.         (save-excursion (beginning-of-line) 
  2511.                 ;; (setq bol (point))
  2512.                 (and auto-fill-inhibit-regexp
  2513.                  (looking-at auto-fill-inhibit-regexp))))
  2514.     nil ;; Auto-filling not required
  2515.       (if (memq justify '(full center right))
  2516.       (save-excursion (unjustify-current-line)))
  2517.  
  2518.       ;; Choose a fill-prefix automatically.
  2519.       (if (and adaptive-fill-mode
  2520.            (or (null fill-prefix) (string= fill-prefix "")))
  2521.       (let ((prefix
  2522.          (fill-context-prefix
  2523.           (save-excursion (backward-paragraph 1) (point))
  2524.           (save-excursion (forward-paragraph 1) (point))
  2525.           ;; Don't accept a non-whitespace fill prefix
  2526.           ;; from the first line of a paragraph.
  2527.           "^[ \t]*$")))
  2528.         (and prefix (not (equal prefix ""))
  2529.          (setq fill-prefix prefix))))
  2530.  
  2531.       (while (and (not give-up) (> (current-column) fc))
  2532.     ;; Determine where to split the line.
  2533.     (let ((fill-point
  2534.            (let ((opoint (point))
  2535.              bounce
  2536.              (first t))
  2537.          (save-excursion
  2538.            (move-to-column (1+ fc))
  2539.            ;; Move back to a word boundary.
  2540.            (while (or first
  2541.                   ;; If this is after period and a single space,
  2542.                   ;; move back once more--we don't want to break
  2543.                   ;; the line there and make it look like a
  2544.                   ;; sentence end.
  2545.                   (and (not (bobp))
  2546.                    (not bounce)
  2547.                    sentence-end-double-space
  2548.                    (save-excursion (forward-char -1)
  2549.                            (and (looking-at "\\. ")
  2550.                             (not (looking-at "\\.  "))))))
  2551.              (setq first nil)
  2552.              (skip-chars-backward "^ \t\n")
  2553.              ;; If we find nowhere on the line to break it,
  2554.              ;; break after one word.  Set bounce to t
  2555.              ;; so we will not keep going in this while loop.
  2556.              (if (bolp)
  2557.              (progn
  2558.                (re-search-forward "[ \t]" opoint t)
  2559.                (setq bounce t)))
  2560.              (skip-chars-backward " \t"))
  2561.            ;; Let fill-point be set to the place where we end up.
  2562.            (point)))))
  2563.       ;; If that place is not the beginning of the line,
  2564.       ;; break the line there.
  2565.       (if (save-excursion
  2566.         (goto-char fill-point)
  2567.         (not (bolp)))
  2568.           (let ((prev-column (current-column)))
  2569.         ;; If point is at the fill-point, do not `save-excursion'.
  2570.         ;; Otherwise, if a comment prefix or fill-prefix is inserted,
  2571.         ;; point will end up before it rather than after it.
  2572.         (if (save-excursion
  2573.               (skip-chars-backward " \t")
  2574.               (= (point) fill-point))
  2575.             (funcall comment-line-break-function t)
  2576.           (save-excursion
  2577.             (goto-char fill-point)
  2578.             (funcall comment-line-break-function t)))
  2579.         ;; Now do justification, if required
  2580.         (if (not (eq justify 'left))
  2581.             (save-excursion 
  2582.               (end-of-line 0)
  2583.               (justify-current-line justify nil t)))
  2584.         ;; If making the new line didn't reduce the hpos of
  2585.         ;; the end of the line, then give up now;
  2586.         ;; trying again will not help.
  2587.         (if (>= (current-column) prev-column)
  2588.             (setq give-up t)))
  2589.         ;; No place to break => stop trying.
  2590.         (setq give-up t))))
  2591.       ;; Justify last line.
  2592.       (justify-current-line justify t t)
  2593.       t)))
  2594.  
  2595. (defvar normal-auto-fill-function 'do-auto-fill
  2596.   "The function to use for `auto-fill-function' if Auto Fill mode is turned on.
  2597. Some major modes set this.")
  2598.  
  2599. (defun auto-fill-mode (&optional arg)
  2600.   "Toggle auto-fill mode.
  2601. With arg, turn auto-fill mode on if and only if arg is positive.
  2602. In Auto-Fill mode, inserting a space at a column beyond `current-fill-column'
  2603. automatically breaks the line at a previous space.
  2604.  
  2605. The value of `normal-auto-fill-function' specifies the function to use
  2606. for `auto-fill-function' when turning Auto Fill mode on."
  2607.   (interactive "P")
  2608.   (prog1 (setq auto-fill-function
  2609.            (if (if (null arg)
  2610.                (not auto-fill-function)
  2611.                (> (prefix-numeric-value arg) 0))
  2612.            normal-auto-fill-function
  2613.            nil))
  2614.     (redraw-modeline)))
  2615.  
  2616. ;; This holds a document string used to document auto-fill-mode.
  2617. (defun auto-fill-function ()
  2618.   "Automatically break line at a previous space, in insertion of text."
  2619.   nil)
  2620.  
  2621. (defun turn-on-auto-fill ()
  2622.   "Unconditionally turn on Auto Fill mode."
  2623.   (auto-fill-mode 1))
  2624.  
  2625. (defun set-fill-column (arg)
  2626.   "Set `fill-column' to specified argument.
  2627. Just \\[universal-argument] as argument means to use the current column
  2628. The variable `fill-column' has a separate value for each buffer."
  2629.   (interactive "_P") ; XEmacs
  2630.   (cond ((integerp arg)
  2631.      (setq fill-column arg))
  2632.     ((consp arg)
  2633.      (setq fill-column (current-column)))
  2634.     ;; Disallow missing argument; it's probably a typo for C-x C-f.
  2635.     (t
  2636.      (error "set-fill-column requires an explicit argument")))
  2637.   (display-message 'command (format "fill-column set to %d" fill-column)))
  2638.  
  2639. (defcustom comment-multi-line t ; XEmacs - this works well with adaptive fill
  2640.   "*Non-nil means \\[indent-new-comment-line] should continue same comment
  2641. on new line, with no new terminator or starter.
  2642. This is obsolete because you might as well use \\[newline-and-indent]."
  2643.   :type 'boolean
  2644.   :group 'fill-comments)
  2645.  
  2646. (defun indent-new-comment-line (&optional soft)
  2647.   "Break line at point and indent, continuing comment if within one.
  2648. This indents the body of the continued comment
  2649. under the previous comment line.
  2650.  
  2651. This command is intended for styles where you write a comment per line,
  2652. starting a new comment (and terminating it if necessary) on each line.
  2653. If you want to continue one comment across several lines, use \\[newline-and-indent].
  2654.  
  2655. If a fill column is specified, it overrides the use of the comment column
  2656. or comment indentation.
  2657.  
  2658. The inserted newline is marked hard if `use-hard-newlines' is true, 
  2659. unless optional argument SOFT is non-nil."
  2660.   (interactive)
  2661.   (let (comcol comstart)
  2662.     (skip-chars-backward " \t")
  2663.     ;; 97/3/14 jhod: Kinsoku processing
  2664.     (if (featurep 'mule)
  2665.     (kinsoku-process))
  2666.     (delete-region (point)
  2667.            (progn (skip-chars-forward " \t")
  2668.               (point)))
  2669.     (if soft (insert ?\n) (newline 1))
  2670.     (if fill-prefix
  2671.     (progn
  2672.       (indent-to-left-margin)
  2673.       (insert fill-prefix))
  2674.     ;; #### - Eric Eide reverts to v18 semantics for this function in
  2675.     ;; fa-extras, which I'm not gonna do.  His changes are to (1) execute
  2676.     ;; the save-excursion below unconditionally, and (2) uncomment the check
  2677.     ;; for (not comment-multi-line) further below.  --Stig 
  2678.       ;;### jhod: probably need to fix this for kinsoku processing
  2679.       (if (not comment-multi-line)
  2680.       (save-excursion
  2681.         (if (and comment-start-skip
  2682.              (let ((opoint (point)))
  2683.                (forward-line -1)
  2684.                (re-search-forward comment-start-skip opoint t)))
  2685.         ;; The old line is a comment.
  2686.         ;; Set WIN to the pos of the comment-start.
  2687.         ;; But if the comment is empty, look at preceding lines
  2688.         ;; to find one that has a nonempty comment.
  2689.  
  2690.         ;; If comment-start-skip contains a \(...\) pair,
  2691.         ;; the real comment delimiter starts at the end of that pair.
  2692.         (let ((win (or (match-end 1) (match-beginning 0))))
  2693.           (while (and (eolp) (not (bobp))
  2694.                   (let (opoint)
  2695.                 (beginning-of-line)
  2696.                 (setq opoint (point))
  2697.                 (forward-line -1)
  2698.                 (re-search-forward comment-start-skip opoint t)))
  2699.             (setq win (or (match-end 1) (match-beginning 0))))
  2700.           ;; Indent this line like what we found.
  2701.           (goto-char win)
  2702.           (setq comcol (current-column))
  2703.           (setq comstart
  2704.             (buffer-substring (point) (match-end 0)))))))
  2705.       (if (and comcol (not fill-prefix))  ; XEmacs - (ENE) from fa-extras.
  2706.       (let ((comment-column comcol)
  2707.         (comment-start comstart)
  2708.         (comment-end comment-end))
  2709.         (and comment-end (not (equal comment-end ""))
  2710.   ;           (if (not comment-multi-line)
  2711.              (progn
  2712.                (forward-char -1)
  2713.                (insert comment-end)
  2714.                (forward-char 1))
  2715.   ;         (setq comment-column (+ comment-column (length comment-start))
  2716.   ;               comment-start "")
  2717.   ;           )
  2718.          )
  2719.         (if (not (eolp))
  2720.         (setq comment-end ""))
  2721.         (insert ?\n)
  2722.         (forward-char -1)
  2723.         (indent-for-comment)
  2724.         (save-excursion
  2725.           ;; Make sure we delete the newline inserted above.
  2726.           (end-of-line)
  2727.           (delete-char 1)))
  2728.     (indent-according-to-mode)))))
  2729.  
  2730.  
  2731. (defun set-selective-display (arg)
  2732.   "Set `selective-display' to ARG; clear it if no arg.
  2733. When the value of `selective-display' is a number > 0,
  2734. lines whose indentation is >= that value are not displayed.
  2735. The variable `selective-display' has a separate value for each buffer."
  2736.   (interactive "P")
  2737.   (if (eq selective-display t)
  2738.       (error "selective-display already in use for marked lines"))
  2739.   (let ((current-vpos
  2740.      (save-restriction
  2741.        (narrow-to-region (point-min) (point))
  2742.        (goto-char (window-start))
  2743.        (vertical-motion (window-height)))))
  2744.     (setq selective-display
  2745.       (and arg (prefix-numeric-value arg)))
  2746.     (recenter current-vpos))
  2747.   (set-window-start (selected-window) (window-start (selected-window)))
  2748.   ;; #### doesn't localize properly:
  2749.   (princ "selective-display set to " t)
  2750.   (prin1 selective-display t)
  2751.   (princ "." t))
  2752.  
  2753. ;; XEmacs
  2754. (defun nuke-selective-display ()
  2755.   "Ensure that the buffer is not in selective-display mode.
  2756. If `selective-display' is t, then restore the buffer text to it's original
  2757. state before disabling selective display." 
  2758.   ;; by Stig@hackvan.com
  2759.   (interactive)
  2760.   (and (eq t selective-display)
  2761.        (save-excursion
  2762.      (save-restriction
  2763.        (widen)
  2764.        (goto-char (point-min))
  2765.        (let ((mod-p (buffer-modified-p))
  2766.          (buffer-read-only nil))
  2767.          (while (search-forward "\r" nil t)
  2768.            (delete-char -1)
  2769.            (insert "\n"))
  2770.          (set-buffer-modified-p mod-p)
  2771.          ))))
  2772.   (setq selective-display nil))
  2773.  
  2774. (add-hook 'change-major-mode-hook 'nuke-selective-display)
  2775.  
  2776. (defconst overwrite-mode-textual (purecopy " Ovwrt")
  2777.   "The string displayed in the mode line when in overwrite mode.")
  2778. (defconst overwrite-mode-binary (purecopy " Bin Ovwrt")
  2779.   "The string displayed in the mode line when in binary overwrite mode.")
  2780.  
  2781. (defun overwrite-mode (arg)
  2782.   "Toggle overwrite mode.
  2783. With arg, turn overwrite mode on iff arg is positive.
  2784. In overwrite mode, printing characters typed in replace existing text
  2785. on a one-for-one basis, rather than pushing it to the right.  At the
  2786. end of a line, such characters extend the line.  Before a tab,
  2787. such characters insert until the tab is filled in.
  2788. \\[quoted-insert] still inserts characters in overwrite mode; this
  2789. is supposed to make it easier to insert characters when necessary."
  2790.   (interactive "P")
  2791.   (setq overwrite-mode
  2792.     (if (if (null arg) (not overwrite-mode)
  2793.           (> (prefix-numeric-value arg) 0))
  2794.         'overwrite-mode-textual))
  2795.   (redraw-modeline))
  2796.  
  2797. (defun binary-overwrite-mode (arg)
  2798.   "Toggle binary overwrite mode.
  2799. With arg, turn binary overwrite mode on iff arg is positive.
  2800. In binary overwrite mode, printing characters typed in replace
  2801. existing text.  Newlines are not treated specially, so typing at the
  2802. end of a line joins the line to the next, with the typed character
  2803. between them.  Typing before a tab character simply replaces the tab
  2804. with the character typed.
  2805. \\[quoted-insert] replaces the text at the cursor, just as ordinary
  2806. typing characters do.
  2807.  
  2808. Note that binary overwrite mode is not its own minor mode; it is a
  2809. specialization of overwrite-mode, entered by setting the
  2810. `overwrite-mode' variable to `overwrite-mode-binary'."
  2811.   (interactive "P")
  2812.   (setq overwrite-mode
  2813.     (if (if (null arg)
  2814.         (not (eq overwrite-mode 'overwrite-mode-binary))
  2815.           (> (prefix-numeric-value arg) 0))
  2816.         'overwrite-mode-binary))
  2817.   (redraw-modeline))
  2818.  
  2819. (defcustom line-number-mode nil
  2820.   "*Non-nil means display line number in modeline."
  2821.   :type 'boolean
  2822.   :group 'editing-basics)
  2823.  
  2824. (defun line-number-mode (arg)
  2825.   "Toggle Line Number mode.
  2826. With arg, turn Line Number mode on iff arg is positive.
  2827. When Line Number mode is enabled, the line number appears
  2828. in the mode line."
  2829.   (interactive "P")
  2830.   (setq line-number-mode
  2831.     (if (null arg) (not line-number-mode)
  2832.       (> (prefix-numeric-value arg) 0)))
  2833.   (redraw-modeline))
  2834.  
  2835. (defcustom column-number-mode nil
  2836.   "*Non-nil means display column number in mode line."
  2837.   :type 'boolean
  2838.   :group 'editing-basics)
  2839.  
  2840. (defun column-number-mode (arg)
  2841.   "Toggle Column Number mode.
  2842. With arg, turn Column Number mode on iff arg is positive.
  2843. When Column Number mode is enabled, the column number appears
  2844. in the mode line."
  2845.   (interactive "P")
  2846.   (setq column-number-mode
  2847.     (if (null arg) (not column-number-mode)
  2848.       (> (prefix-numeric-value arg) 0)))
  2849.   (redraw-modeline))
  2850.  
  2851.  
  2852. (defcustom blink-matching-paren t
  2853.   "*Non-nil means show matching open-paren when close-paren is inserted."
  2854.   :type 'boolean
  2855.   :group 'paren-blinking)
  2856.  
  2857. (defcustom blink-matching-paren-on-screen t
  2858.   "*Non-nil means show matching open-paren when it is on screen.
  2859. nil means don't show it (but the open-paren can still be shown
  2860. when it is off screen."
  2861.   :type 'boolean
  2862.   :group 'paren-blinking)
  2863.  
  2864. (defcustom blink-matching-paren-distance 12000
  2865.   "*If non-nil, is maximum distance to search for matching open-paren."
  2866.   :type '(choice integer (const nil))
  2867.   :group 'paren-blinking)
  2868.  
  2869. (defcustom blink-matching-delay 1
  2870.   "*The number of seconds that `blink-matching-open' will delay at a match."
  2871.   :type 'number
  2872.   :group 'paren-blinking)
  2873.  
  2874. (defcustom blink-matching-paren-dont-ignore-comments nil
  2875.   "*Non-nil means `blink-matching-paren' should not ignore comments."
  2876.   :type 'boolean
  2877.   :group 'paren-blinking)
  2878.  
  2879. (defun blink-matching-open ()
  2880.   "Move cursor momentarily to the beginning of the sexp before point."
  2881.   (interactive "_") ; XEmacs
  2882.   (and (> (point) (1+ (point-min)))
  2883.        blink-matching-paren
  2884.        ;; Verify an even number of quoting characters precede the close.
  2885.        (= 1 (logand 1 (- (point)
  2886.              (save-excursion
  2887.                (forward-char -1)
  2888.                (skip-syntax-backward "/\\")
  2889.                (point)))))
  2890.        (let* ((oldpos (point))
  2891.           (blinkpos)
  2892.           (mismatch))
  2893.      (save-excursion
  2894.        (save-restriction
  2895.          (if blink-matching-paren-distance
  2896.          (narrow-to-region (max (point-min)
  2897.                     (- (point) blink-matching-paren-distance))
  2898.                    oldpos))
  2899.          (condition-case ()
  2900.          (let ((parse-sexp-ignore-comments
  2901.             (and parse-sexp-ignore-comments
  2902.                  (not blink-matching-paren-dont-ignore-comments))))
  2903.            (setq blinkpos (scan-sexps oldpos -1)))
  2904.            (error nil)))
  2905.        (and blinkpos
  2906.         (/= (char-syntax (char-after blinkpos))
  2907.             ?\$)
  2908.         (setq mismatch
  2909.               (or (null (matching-paren (char-after blinkpos)))
  2910.               (/= (char-after (1- oldpos))
  2911.                   (matching-paren (char-after blinkpos))))))
  2912.        (if mismatch (setq blinkpos nil))
  2913.        (if blinkpos
  2914.            (progn
  2915.         (goto-char blinkpos)
  2916.         (if (pos-visible-in-window-p)
  2917.             (and blink-matching-paren-on-screen
  2918.              (progn
  2919.                (auto-show-make-point-visible)
  2920.                (sit-for blink-matching-delay)))
  2921.           (goto-char blinkpos)
  2922.           (display-message
  2923.            'command
  2924.            (format
  2925.             "Matches %s"
  2926.             ;; Show what precedes the open in its line, if anything.
  2927.             (if (save-excursion
  2928.               (skip-chars-backward " \t")
  2929.               (not (bolp)))
  2930.             (buffer-substring (progn (beginning-of-line) (point))
  2931.                       (1+ blinkpos))
  2932.               ;; Show what follows the open in its line, if anything.
  2933.               (if (save-excursion
  2934.                 (forward-char 1)
  2935.                 (skip-chars-forward " \t")
  2936.                 (not (eolp)))
  2937.               (buffer-substring blinkpos
  2938.                         (progn (end-of-line) (point)))
  2939.             ;; Otherwise show the previous nonblank line,
  2940.             ;; if there is one.
  2941.             (if (save-excursion
  2942.                   (skip-chars-backward "\n \t")
  2943.                   (not (bobp)))
  2944.                 (concat
  2945.                  (buffer-substring (progn
  2946.                          (skip-chars-backward "\n \t")
  2947.                          (beginning-of-line)
  2948.                          (point))
  2949.                            (progn (end-of-line)
  2950.                               (skip-chars-backward " \t")
  2951.                               (point)))
  2952.                  ;; Replace the newline and other whitespace with `...'.
  2953.                  "..."
  2954.                  (buffer-substring blinkpos (1+ blinkpos)))
  2955.               ;; There is nothing to show except the char itself.
  2956.               (buffer-substring blinkpos (1+ blinkpos)))))))))
  2957.          (cond (mismatch
  2958.             (display-message 'no-log "Mismatched parentheses"))
  2959.            ((not blink-matching-paren-distance)
  2960.             (display-message 'no-log "Unmatched parenthesis"))))))))
  2961.  
  2962. ;Turned off because it makes dbx bomb out.
  2963. (setq blink-paren-function 'blink-matching-open)
  2964.  
  2965. (eval-when-compile (defvar myhelp))    ; suppress compiler warning
  2966.  
  2967. ;; XEmacs: Some functions moved to cmdloop.el:
  2968. ;; keyboard-quit
  2969. ;; buffer-quit-function
  2970. ;; keyboard-escape-quit
  2971.  
  2972. (defun assoc-ignore-case (key alist)
  2973.   "Like `assoc', but assumes KEY is a string and ignores case when comparing."
  2974.   (setq key (downcase key))
  2975.   (let (element)
  2976.     (while (and alist (not element))
  2977.       (if (equal key (downcase (car (car alist))))
  2978.       (setq element (car alist)))
  2979.       (setq alist (cdr alist)))
  2980.     element))
  2981.  
  2982.  
  2983. (defcustom mail-user-agent 'sendmail-user-agent
  2984.   "*Your preference for a mail composition package.
  2985. Various Emacs Lisp packages (e.g. reporter) require you to compose an
  2986. outgoing email message.  This variable lets you specify which
  2987. mail-sending package you prefer.
  2988.  
  2989. Valid values include:
  2990.  
  2991.     sendmail-user-agent -- use the default Emacs Mail package
  2992.     mh-e-user-agent     -- use the Emacs interface to the MH mail system
  2993.     message-user-agent  -- use the GNUS mail sending package
  2994.  
  2995. Additional valid symbols may be available; check with the author of
  2996. your package for details."
  2997.   :type '(radio (function-item :tag "Default Emacs mail"
  2998.                    :format "%t\n"
  2999.                    sendmail-user-agent)
  3000.         (function-item :tag "Gnus mail sending package"
  3001.                    :format "%t\n"
  3002.                    message-user-agent)
  3003.         (function :tag "Other"))
  3004.   :group 'mail)
  3005.  
  3006. (defun define-mail-user-agent (symbol composefunc sendfunc
  3007.                       &optional abortfunc hookvar)
  3008.   "Define a symbol to identify a mail-sending package for `mail-user-agent'.
  3009.  
  3010. SYMBOL can be any Lisp symbol.  Its function definition and/or
  3011. value as a variable do not matter for this usage; we use only certain
  3012. properties on its property list, to encode the rest of the arguments.
  3013.  
  3014. COMPOSEFUNC is program callable function that composes an outgoing
  3015. mail message buffer.  This function should set up the basics of the
  3016. buffer without requiring user interaction.  It should populate the
  3017. standard mail headers, leaving the `to:' and `subject:' headers blank
  3018. by default.
  3019.  
  3020. COMPOSEFUNC should accept several optional arguments--the same
  3021. arguments that `compose-mail' takes.  See that function's documentation.
  3022.  
  3023. SENDFUNC is the command a user would run to send the message.
  3024.  
  3025. Optional ABORTFUNC is the command a user would run to abort the
  3026. message.  For mail packages that don't have a separate abort function,
  3027. this can be `kill-buffer' (the equivalent of omitting this argument).
  3028.  
  3029. Optional HOOKVAR is a hook variable that gets run before the message
  3030. is actually sent.  Callers that use the `mail-user-agent' may
  3031. install a hook function temporarily on this hook variable.
  3032. If HOOKVAR is nil, `mail-send-hook' is used.
  3033.  
  3034. The properties used on SYMBOL are `composefunc', `sendfunc',
  3035. `abortfunc', and `hookvar'."
  3036.   (put symbol 'composefunc composefunc)
  3037.   (put symbol 'sendfunc sendfunc)
  3038.   (put symbol 'abortfunc (or abortfunc 'kill-buffer))
  3039.   (put symbol 'hookvar (or hookvar 'mail-send-hook)))
  3040.  
  3041. (define-mail-user-agent 'sendmail-user-agent
  3042.   'sendmail-user-agent-compose 'mail-send-and-exit)
  3043.  
  3044. (define-mail-user-agent 'message-user-agent
  3045.   'message-mail 'message-send-and-exit
  3046.   'message-kill-buffer 'message-send-hook)
  3047.  
  3048. (defun sendmail-user-agent-compose (&optional to subject other-headers continue
  3049.                           switch-function yank-action
  3050.                           send-actions)
  3051.   (if switch-function
  3052.       (let ((special-display-buffer-names nil)
  3053.         (special-display-regexps nil)
  3054.         (same-window-buffer-names nil)
  3055.         (same-window-regexps nil))
  3056.     (funcall switch-function "*mail*")))
  3057.   (let ((cc (cdr (assoc-ignore-case "cc" other-headers)))
  3058.     (in-reply-to (cdr (assoc-ignore-case "in-reply-to" other-headers))))
  3059.     (or (mail continue to subject in-reply-to cc yank-action send-actions)
  3060.     continue
  3061.     (error "Message aborted"))
  3062.     (save-excursion
  3063.       (goto-char (point-min))
  3064.       (search-forward mail-header-separator)
  3065.       (beginning-of-line)
  3066.       (while other-headers
  3067.     (if (not (member (car (car other-headers)) '("in-reply-to" "cc")))
  3068.         (insert (car (car other-headers)) ": "
  3069.             (cdr (car other-headers)) "\n"))
  3070.     (setq other-headers (cdr other-headers)))
  3071.       t)))
  3072.  
  3073. (define-mail-user-agent 'mh-e-user-agent
  3074.   'mh-user-agent-compose 'mh-send-letter 'mh-fully-kill-draft
  3075.   'mh-before-send-letter-hook)
  3076.  
  3077. (defun compose-mail (&optional to subject other-headers continue
  3078.                    switch-function yank-action send-actions)
  3079.   "Start composing a mail message to send.
  3080. This uses the user's chosen mail composition package
  3081. as selected with the variable `mail-user-agent'.
  3082. The optional arguments TO and SUBJECT specify recipients
  3083. and the initial Subject field, respectively.
  3084.  
  3085. OTHER-HEADERS is an alist specifying additional
  3086. header fields.  Elements look like (HEADER . VALUE) where both
  3087. HEADER and VALUE are strings.
  3088.  
  3089. CONTINUE, if non-nil, says to continue editing a message already
  3090. being composed.
  3091.  
  3092. SWITCH-FUNCTION, if non-nil, is a function to use to
  3093. switch to and display the buffer used for mail composition.
  3094.  
  3095. YANK-ACTION, if non-nil, is an action to perform, if and when necessary,
  3096. to insert the raw text of the message being replied to.
  3097. It has the form (FUNCTION . ARGS).  The user agent will apply
  3098. FUNCTION to ARGS, to insert the raw text of the original message.
  3099. \(The user agent will also run `mail-citation-hook', *after* the
  3100. original text has been inserted in this way.)
  3101.  
  3102. SEND-ACTIONS is a list of actions to call when the message is sent.
  3103. Each action has the form (FUNCTION . ARGS)."
  3104.   (interactive
  3105.    (list nil nil nil current-prefix-arg))
  3106.   (let ((function (get mail-user-agent 'composefunc)))
  3107.     (funcall function to subject other-headers continue
  3108.          switch-function yank-action send-actions)))
  3109.  
  3110. (defun compose-mail-other-window (&optional to subject other-headers continue
  3111.                         yank-action send-actions)
  3112.   "Like \\[compose-mail], but edit the outgoing message in another window."
  3113.   (interactive
  3114.    (list nil nil nil current-prefix-arg))
  3115.   (compose-mail to subject other-headers continue
  3116.         'switch-to-buffer-other-window yank-action send-actions))
  3117.  
  3118.  
  3119. (defun compose-mail-other-frame (&optional to subject other-headers continue
  3120.                         yank-action send-actions)
  3121.   "Like \\[compose-mail], but edit the outgoing message in another frame."
  3122.   (interactive
  3123.    (list nil nil nil current-prefix-arg))
  3124.   (compose-mail to subject other-headers continue
  3125.         'switch-to-buffer-other-frame yank-action send-actions))
  3126.  
  3127.  
  3128. (defun set-variable (var val)
  3129.   "Set VARIABLE to VALUE.  VALUE is a Lisp object.
  3130. When using this interactively, supply a Lisp expression for VALUE.
  3131. If you want VALUE to be a string, you must surround it with doublequotes.
  3132.  
  3133. If VARIABLE has a `variable-interactive' property, that is used as if
  3134. it were the arg to `interactive' (which see) to interactively read the value."
  3135.   (interactive
  3136.    (let* ((var (read-variable "Set variable: "))
  3137.       ;; #### - yucky code replication here.  This should use something
  3138.       ;; from help.el or hyper-apropos.el 
  3139.       (minibuffer-help-form
  3140.        '(funcall myhelp))
  3141.       (myhelp
  3142.        #'(lambda ()
  3143.           (with-output-to-temp-buffer "*Help*"
  3144.         (prin1 var)
  3145.         (princ "\nDocumentation:\n")
  3146.         (princ (substring (documentation-property var 'variable-documentation)
  3147.                   1))
  3148.         (if (boundp var)
  3149.             (let ((print-length 20))
  3150.               (princ "\n\nCurrent value: ")
  3151.               (prin1 (symbol-value var))))
  3152.         (save-excursion
  3153.           (set-buffer standard-output)
  3154.           (help-mode))
  3155.         nil))))
  3156.      (list var
  3157.        (let ((prop (get var 'variable-interactive)))
  3158.          (if prop
  3159.          ;; Use VAR's `variable-interactive' property
  3160.          ;; as an interactive spec for prompting.
  3161.          (call-interactively (list 'lambda '(arg)
  3162.                        (list 'interactive prop)
  3163.                        'arg))
  3164.            (eval-minibuffer (format "Set %s to value: " var)))))))
  3165.   (set var val))
  3166.  
  3167. ;; XEmacs
  3168. (defun activate-region ()
  3169.   "Activate the region, if `zmacs-regions' is true.
  3170. Setting `zmacs-regions' to true causes LISPM-style active regions to be used.
  3171. This function has no effect if `zmacs-regions' is false."
  3172.   (interactive)
  3173.   (and zmacs-regions (zmacs-activate-region)))
  3174.  
  3175. ;; XEmacs
  3176. (defsubst region-exists-p ()
  3177.   "Non-nil iff the region exists.
  3178. If active regions are in use (i.e. `zmacs-regions' is true), this means that
  3179.  the region is active.  Otherwise, this means that the user has pushed
  3180.  a mark in this buffer at some point in the past.
  3181. The functions `region-beginning' and `region-end' can be used to find the
  3182.  limits of the region."
  3183.   (not (null (mark))))
  3184.  
  3185. ;; XEmacs
  3186. (defun region-active-p ()
  3187.   "Non-nil iff the region is active.
  3188. If `zmacs-regions' is true, this is equivalent to `region-exists-p'.
  3189. Otherwise, this function always returns false."
  3190.   (and zmacs-regions zmacs-region-extent))
  3191.  
  3192. ;; A bunch of stuff was moved elsewhere:
  3193. ;; completion-list-mode-map
  3194. ;; completion-reference-buffer
  3195. ;; completion-base-size
  3196. ;; delete-completion-window
  3197. ;; previous-completion
  3198. ;; next-completion
  3199. ;; choose-completion
  3200. ;; choose-completion-delete-max-match
  3201. ;; choose-completion-string
  3202. ;; completion-list-mode
  3203. ;; completion-fixup-function
  3204. ;; completion-setup-function
  3205. ;; switch-to-completions
  3206. ;; event stuffs
  3207. ;; keypad stuffs
  3208.  
  3209. ;; The rest of this file is not in Lisp in FSF
  3210. (defun capitalize-region-or-word (arg)
  3211.   "Capitalize the selected region or the following word (or ARG words)."
  3212.   (interactive "p")
  3213.   (if (region-active-p)
  3214.       (capitalize-region (region-beginning) (region-end))
  3215.     (capitalize-word arg)))
  3216.  
  3217. (defun upcase-region-or-word (arg)
  3218.   "Upcase the selected region or the following word (or ARG words)."
  3219.   (interactive "p")
  3220.   (if (region-active-p)
  3221.       (upcase-region (region-beginning) (region-end))
  3222.     (upcase-word arg)))
  3223.  
  3224. (defun downcase-region-or-word (arg)
  3225.   "Downcase the selected region or the following word (or ARG words)."
  3226.   (interactive "p")
  3227.   (if (region-active-p)
  3228.       (downcase-region (region-beginning) (region-end))
  3229.     (downcase-word arg)))
  3230.  
  3231. ;;;
  3232. ;;; Most of the zmacs code is now in elisp.  The only thing left in C
  3233. ;;; are the variables zmacs-regions, zmacs-region-active-p and
  3234. ;;; zmacs-region-stays plus the function zmacs_update_region which
  3235. ;;; calls the lisp level zmacs-update-region.  It must remain since it
  3236. ;;; must be called by core C code.
  3237. ;;;
  3238. ;;; Huh?  Why couldn't "core C code" just use
  3239. ;;; call0(Qzmacs_update_region)??? -hniksic
  3240.  
  3241. (defvar zmacs-activate-region-hook nil
  3242.   "Function or functions called when the region becomes active;
  3243. see the variable `zmacs-regions'.")
  3244.  
  3245. (defvar zmacs-deactivate-region-hook nil
  3246.   "Function or functions called when the region becomes inactive;
  3247. see the variable `zmacs-regions'.")
  3248.  
  3249. (defvar zmacs-update-region-hook nil
  3250.   "Function or functions called when the active region changes.
  3251. This is called after each command that sets `zmacs-region-stays' to t.
  3252. See the variable `zmacs-regions'.")
  3253.  
  3254. (defvar zmacs-region-extent nil
  3255.   "The extent of the zmacs region; don't use this.")
  3256.  
  3257. (defvar zmacs-region-rectangular-p nil
  3258.   "Whether the zmacs region is a rectangle; don't use this.")
  3259.  
  3260. (defun zmacs-make-extent-for-region (region)
  3261.   ;; Given a region, this makes an extent in the buffer which holds that
  3262.   ;; region, for highlighting purposes.  If the region isn't associated
  3263.   ;; with a buffer, this does nothing.
  3264.   (let ((buffer nil)
  3265.     (valid (and (extentp zmacs-region-extent)
  3266.             (extent-object zmacs-region-extent)
  3267.             (buffer-live-p (extent-object zmacs-region-extent))))
  3268.     start end)
  3269.     (cond ((consp region)
  3270.        (setq start (min (car region) (cdr region))
  3271.          end (max (car region) (cdr region))
  3272.          valid (and valid
  3273.                 (eq (marker-buffer (car region))
  3274.                 (extent-object zmacs-region-extent)))
  3275.          buffer (marker-buffer (car region))))
  3276.       (t
  3277.        (signal 'error (list "Invalid region" region))))
  3278.  
  3279.     (if valid
  3280.     nil
  3281.       ;; The condition case is in case any of the extents are dead or
  3282.       ;; otherwise incapacitated.
  3283.       (condition-case ()
  3284.       (if (listp zmacs-region-extent)
  3285.           (mapc 'delete-extent zmacs-region-extent)
  3286.         (delete-extent zmacs-region-extent))
  3287.     (error nil)))
  3288.  
  3289.     (if valid
  3290.     (set-extent-endpoints zmacs-region-extent start end)
  3291.       (setq zmacs-region-extent (make-extent start end buffer))
  3292.  
  3293.       ;; Make the extent be closed on the right, which means that if
  3294.       ;; characters are inserted exactly at the end of the extent, the
  3295.       ;; extent will grow to cover them.  This is important for shell
  3296.       ;; buffers - suppose one makes a region, and one end is at point-max.
  3297.       ;; If the shell produces output, that marker will remain at point-max
  3298.       ;; (its position will increase).  So it's important that the extent
  3299.       ;; exhibit the same behavior, lest the region covered by the extent
  3300.       ;; (the visual indication), and the region between point and mark
  3301.       ;; (the actual region value) become different!
  3302.       (set-extent-property zmacs-region-extent 'end-open nil)
  3303.  
  3304.       ;; use same priority as mouse-highlighting so that conflicts between
  3305.       ;; the region extent and a mouse-highlighted extent are resolved by
  3306.       ;; the usual size-and-endpoint-comparison method.
  3307.       (set-extent-priority zmacs-region-extent mouse-highlight-priority)
  3308.       (set-extent-face zmacs-region-extent 'zmacs-region)
  3309.  
  3310.       ;; #### It might be better to actually break
  3311.       ;; default-mouse-track-next-move-rect out of mouse.el so that we
  3312.       ;; can use its logic here.
  3313.       (cond
  3314.        (zmacs-region-rectangular-p
  3315.     (setq zmacs-region-extent (list zmacs-region-extent))
  3316.     (default-mouse-track-next-move-rect start end zmacs-region-extent)
  3317.     ))
  3318.  
  3319.       zmacs-region-extent)))
  3320.  
  3321. (defun zmacs-region-buffer ()
  3322.   "Return the buffer containing the zmacs region, or nil."
  3323.   ;; #### this is horrible and kludgy!  This stuff needs to be rethought.
  3324.   (and zmacs-regions zmacs-region-active-p
  3325.        (or (marker-buffer (mark-marker t))
  3326.        (and (extent-live-p zmacs-region-extent)
  3327.             (buffer-live-p (extent-object zmacs-region-extent))
  3328.             (extent-object zmacs-region-extent)))))
  3329.  
  3330. (defun zmacs-activate-region ()
  3331.   "Make the region between `point' and `mark' be active (highlighted),
  3332. if `zmacs-regions' is true.  Only a very small number of commands
  3333. should ever do this.  Calling this function will call the hook
  3334. `zmacs-activate-region-hook', if the region was previously inactive.
  3335. Calling this function ensures that the region stays active after the
  3336. current command terminates, even if `zmacs-region-stays' is not set.
  3337. Returns t if the region was activated (i.e. if `zmacs-regions' if t)."
  3338.   (if (not zmacs-regions)
  3339.       nil
  3340.     (setq zmacs-region-active-p t
  3341.       zmacs-region-stays t
  3342.       zmacs-region-rectangular-p (and (boundp 'mouse-track-rectangle-p)
  3343.                       mouse-track-rectangle-p))
  3344.     (if (marker-buffer (mark-marker t))
  3345.     (zmacs-make-extent-for-region (cons (point-marker t) (mark-marker t))))
  3346.     (run-hooks 'zmacs-activate-region-hook)
  3347.     t))
  3348.  
  3349. (defun zmacs-deactivate-region ()
  3350.   "Make the region between `point' and `mark' no longer be active,
  3351. if `zmacs-regions' is true.  You shouldn't need to call this; the
  3352. command loop calls it when appropriate.  Calling this function will
  3353. call the hook `zmacs-deactivate-region-hook', if the region was
  3354. previously active.  Returns t if the region had been active, nil
  3355. otherwise."
  3356.   (if (not zmacs-region-active-p)
  3357.       nil
  3358.     (setq zmacs-region-active-p nil
  3359.       zmacs-region-stays nil
  3360.       zmacs-region-rectangular-p nil)
  3361.     (if zmacs-region-extent
  3362.     (let ((inhibit-quit t))
  3363.       (if (listp zmacs-region-extent)
  3364.           (mapc 'delete-extent zmacs-region-extent)
  3365.         (delete-extent zmacs-region-extent))
  3366.       (setq zmacs-region-extent nil)))
  3367.     (run-hooks 'zmacs-deactivate-region-hook)
  3368.     t))
  3369.  
  3370. (defun zmacs-update-region ()
  3371.   "Update the highlighted region between `point' and `mark'.
  3372. You shouldn't need to call this; the command loop calls it
  3373. when appropriate.  Calling this function will call the hook
  3374. `zmacs-update-region-hook', if the region is active."
  3375.   (when zmacs-region-active-p
  3376.     (when (marker-buffer (mark-marker t))
  3377.       (zmacs-make-extent-for-region (cons (point-marker t)
  3378.                       (mark-marker t))))
  3379.     (run-hooks 'zmacs-update-region-hook)))
  3380.  
  3381. ;;;;;;
  3382. ;;;;;; echo area stuff
  3383. ;;;;;;
  3384.  
  3385. ;;; #### Should this be moved to a separate file, for clarity?
  3386. ;;; -hniksic
  3387.  
  3388. ;;; The `message-stack' is an alist of labels with messages; the first
  3389. ;;; message in this list is always in the echo area.  A call to
  3390. ;;; `display-message' inserts a label/message pair at the head of the
  3391. ;;; list, and removes any other pairs with that label.  Calling
  3392. ;;; `clear-message' causes any pair with matching label to be removed,
  3393. ;;; and this may cause the displayed message to change or vanish.  If
  3394. ;;; the label arg is nil, the entire message stack is cleared.
  3395. ;;;
  3396. ;;; Message/error filtering will be a little tricker to implement than
  3397. ;;; logging, since messages can be built up incrementally
  3398. ;;; using clear-message followed by repeated calls to append-message
  3399. ;;; (this happens with error messages).  For messages which aren't
  3400. ;;; created this way, filtering could be implemented at display-message
  3401. ;;; very easily.
  3402. ;;;
  3403. ;;; Bits of the logging code are borrowed from log-messages.el by
  3404. ;;; Robert Potter (rpotter@grip.cis.upenn.edu).
  3405.  
  3406. ;; need this to terminate the currently-displayed message
  3407. ;; ("Loading simple ...")
  3408. (when (and
  3409.        (not (fboundp 'display-message))
  3410.        (not (featurep 'debug)))
  3411.   (send-string-to-terminal "\n"))
  3412.  
  3413. (defvar message-stack nil
  3414.   "An alist of label/string pairs representing active echo-area messages.
  3415. The first element in the list is currently displayed in the echo area.
  3416. Do not modify this directly--use the `message' or 
  3417. `display-message'/`clear-message' functions.")
  3418.  
  3419. (defvar remove-message-hook 'log-message
  3420.   "A function or list of functions to be called when a message is removed
  3421. from the echo area at the bottom of the frame.  The label of the removed
  3422. message is passed as the first argument, and the text of the message
  3423. as the second argument.")
  3424.  
  3425. (defcustom log-message-max-size 50000
  3426.   "Maximum size of the \" *Message-Log*\" buffer.  See `log-message'."
  3427.   :type 'integer
  3428.   :group 'log-message)
  3429. (make-compatible-variable 'message-log-max 'log-message-max-size)
  3430.  
  3431. ;; We used to reject quite a lot of stuff here, but it was a bad idea,
  3432. ;; for two reasons:
  3433. ;;
  3434. ;; a) In most circumstances, you *want* to see the message in the log.
  3435. ;;    The explicitly non-loggable messages should be marked as such by
  3436. ;;    the issuer.  Gratuitous non-displaying of random regexps made
  3437. ;;    debugging harder, too (because various reasonable debugging
  3438. ;;    messages would get eaten).
  3439. ;;
  3440. ;; b) It slowed things down.  Yes, visibly.
  3441. ;;
  3442. ;; So, I left only a few of the really useless ones on this kill-list.
  3443. ;;
  3444. ;;                                            --hniksic
  3445. (defcustom log-message-ignore-regexps
  3446.   '(;; Note: adding entries to this list slows down messaging
  3447.     ;; significantly.  Wherever possible, use message lables.
  3448.  
  3449.     ;; Often-seen messages
  3450.     "\\`\\'"                ; empty message
  3451.     "\\`\\(Beginning\\|End\\) of buffer\\'"
  3452.     ;;"^Quit$"
  3453.     ;; completions
  3454.     ;; Many packages print this -- impossible to categorize
  3455.     ;;"^Making completion list"
  3456.     ;; Gnus
  3457.     ;; "^No news is no news$"
  3458.     ;; "^No more\\( unread\\)? newsgroups$"
  3459.     ;; "^Opening [^ ]+ server\\.\\.\\."
  3460.     ;; "^[^:]+: Reading incoming mail"
  3461.     ;; "^Getting mail from "
  3462.     ;; "^\\(Generating Summary\\|Sorting threads\\|Making sparse threads\\|Scoring\\|Checking new news\\|Expiring articles\\|Sending\\)\\.\\.\\."
  3463.     ;; "^\\(Fetching headers for\\|Retrieving newsgroup\\|Reading active file\\)"
  3464.     ;; "^No more\\( unread\\)? articles"
  3465.     ;; "^Deleting article "
  3466.     ;; W3
  3467.     ;; "^Parsed [0-9]+ of [0-9]+ ([0-9]+%)"
  3468.     )
  3469.   "List of regular expressions matching messages which shouldn't be logged.
  3470. See `log-message'.  
  3471.  
  3472. Ideally, packages which generate messages which might need to be ignored
  3473. should label them with 'progress, 'prompt, or 'no-log, so they can be 
  3474. filtered by the log-message-ignore-labels."
  3475.   :type '(repeat regexp)
  3476.   :group 'log-message)
  3477.  
  3478. (defcustom log-message-ignore-labels 
  3479.   '(help-echo command progress prompt no-log garbage-collecting auto-saving)
  3480.   "List of symbols indicating labels of messages which shouldn't be logged.
  3481. See `display-message' for some common labels.  See also `log-message'."
  3482.   :type '(repeat (symbol :tag "Label"))
  3483.   :group 'log-message)
  3484.  
  3485. ;;Subsumed by view-lossage
  3486. ;; Not really, I'm adding it back by popular demand. -slb
  3487. (defun show-message-log ()
  3488.   "Show the \" *Message-Log*\" buffer, which contains old messages and errors."
  3489.   (interactive)
  3490.   (pop-to-buffer (get-buffer-create " *Message-Log*")))
  3491.  
  3492. (defvar log-message-filter-function 'log-message-filter
  3493.   "Value must be a function of two arguments: a symbol (label) and 
  3494. a string (message).  It should return non-nil to indicate a message
  3495. should be logged.  Possible values include 'log-message-filter and
  3496. 'log-message-filter-errors-only.")
  3497.  
  3498. (defun log-message-filter (label message)
  3499.   "Default value of log-message-filter-function.
  3500. Mesages whose text matches one of the log-message-ignore-regexps
  3501. or whose label appears in log-message-ignore-labels are not saved."
  3502.   (let ((r  log-message-ignore-regexps)
  3503.     (ok (not (memq label log-message-ignore-labels))))
  3504.     (while (and r ok)
  3505.       (if (save-match-data (string-match (car r) message))
  3506.       (setq ok nil))
  3507.       (setq r (cdr r)))
  3508.     ok))
  3509.  
  3510. (defun log-message-filter-errors-only (label message)
  3511.   "For use as the log-message-filter-function.  Only logs error messages."
  3512.   (eq label 'error))
  3513.  
  3514. (defun log-message (label message)
  3515.   "Stuff a copy of the message into the \" *Message-Log*\" buffer,
  3516. if it satisfies the log-message-filter-function.
  3517.  
  3518. For use on remove-message-hook."
  3519.   (if (and (not noninteractive)
  3520.        (funcall log-message-filter-function label message))
  3521.       (save-excursion
  3522.     (set-buffer (get-buffer-create " *Message-Log*"))
  3523.     (goto-char (point-max))
  3524.     ;; (insert (concat (upcase (symbol-name label)) ": "  message "\n"))
  3525.     (insert message "\n")
  3526.     (if (> (point-max) (max log-message-max-size (point-min)))
  3527.         (progn
  3528.           ;; trim log to ~90% of max size
  3529.           (goto-char (max (- (point-max)
  3530.                  (truncate (* 0.9 log-message-max-size)))
  3531.                   (point-min)))
  3532.           (forward-line 1)
  3533.           (delete-region (point-min) (point)))))))
  3534.  
  3535. (defun message-displayed-p (&optional return-string frame)
  3536.   "Return a non-nil value if a message is presently displayed in the\n\
  3537. minibuffer's echo area.  If optional argument RETURN-STRING is non-nil,\n\
  3538. return a string containing the message, otherwise just return t."
  3539.   ;; by definition, a message is displayed if the echo area buffer is
  3540.   ;; non-empty (see also echo_area_active()).  It had better also
  3541.   ;; be the case that message-stack is nil exactly when the echo area
  3542.   ;; is non-empty.
  3543.   (let ((buffer (get-buffer " *Echo Area*")))
  3544.     (and (< (point-min buffer) (point-max buffer))
  3545.      (if return-string
  3546.          (buffer-substring nil nil buffer)
  3547.        t))))
  3548.  
  3549. ;;; Returns the string which remains in the echo area, or nil if none.
  3550. ;;; If label is nil, the whole message stack is cleared.
  3551. (defun clear-message (&optional label frame stdout-p no-restore)
  3552.   "Remove any message with the given LABEL from the message-stack,
  3553. erasing it from the echo area if it's currently displayed there.
  3554. If a message remains at the head of the message-stack and NO-RESTORE
  3555. is nil, it will be displayed.  The string which remains in the echo
  3556. area will be returned, or nil if the message-stack is now empty.
  3557. If LABEL is nil, the entire message-stack is cleared.
  3558.  
  3559. Unless you need the return value or you need to specify a label,
  3560. you should just use (message nil)."
  3561.   (or frame (setq frame (selected-frame)))
  3562.   (let ((clear-stream (and message-stack (eq 'stream (frame-type frame)))))
  3563.     (remove-message label frame)
  3564.     (let ((buffer (get-buffer " *Echo Area*"))
  3565.       (inhibit-read-only t)
  3566.       (zmacs-region-stays zmacs-region-stays)) ; preserve from change
  3567.       (erase-buffer buffer))
  3568.     (if clear-stream
  3569.     (send-string-to-terminal ?\n stdout-p))
  3570.     (if no-restore
  3571.     nil            ; just preparing to put another msg up
  3572.       (if message-stack
  3573.       (let ((oldmsg  (cdr (car message-stack))))
  3574.         (raw-append-message oldmsg frame stdout-p)
  3575.         oldmsg)
  3576.     ;; ### should we (redisplay-echo-area) here?  messes some things up.
  3577.     nil))))
  3578.  
  3579. (defun remove-message (&optional label frame)
  3580.   ;; If label is nil, we want to remove all matching messages.
  3581.   ;; Must reverse the stack first to log them in the right order.
  3582.   (let ((log nil))
  3583.     (while (and message-stack
  3584.         (or (null label)    ; null label means clear whole stack
  3585.             (eq label (car (car message-stack)))))
  3586.       (setq log (cons (car message-stack) log))
  3587.     (setq message-stack (cdr message-stack)))
  3588.     (let ((s  message-stack))
  3589.       (while (cdr s)
  3590.     (let ((msg (car (cdr s))))
  3591.       (if (eq label (car msg))
  3592.           (progn
  3593.         (setq log (cons msg log))
  3594.         (setcdr s (cdr (cdr s))))
  3595.         (setq s (cdr s))))))
  3596.     ;; (possibly) log each removed message
  3597.     (while log
  3598.       (condition-case e
  3599.       (run-hook-with-args 'remove-message-hook
  3600.                   (car (car log)) (cdr (car log)))
  3601.     (error (setq remove-message-hook nil)
  3602.            (message "remove-message-hook error: %s" e)
  3603.            (sit-for 2)
  3604.            (let ((inhibit-read-only t))
  3605.          (erase-buffer (get-buffer " *Echo Area*")))
  3606.            (signal (car e) (cdr e))))
  3607.       (setq log (cdr log)))))
  3608.  
  3609. (defun append-message (label message &optional frame stdout-p)
  3610.   (or frame (setq frame (selected-frame)))
  3611.   ;; add a new entry to the message-stack, or modify an existing one
  3612.   (let ((top (car message-stack)))
  3613.     (if (eq label (car top))
  3614.     (setcdr top (concat (cdr top) message))
  3615.       (setq message-stack (cons (cons label message) message-stack))))
  3616.   (raw-append-message message frame stdout-p))
  3617.  
  3618. ;; really append the message to the echo area.  no fiddling with message-stack.
  3619. (defun raw-append-message (message &optional frame stdout-p)
  3620.   (if (eq message "") nil
  3621.     (let ((buffer (get-buffer " *Echo Area*"))
  3622.       (zmacs-region-stays zmacs-region-stays)) ; preserve from change
  3623.       (save-excursion
  3624.     (set-buffer buffer)
  3625.     (let ((inhibit-read-only t))
  3626.       (insert message)))
  3627.       ;; Conditionalizing on the device type in this way is not that clean,
  3628.       ;; but neither is having a device method, as I originally implemented
  3629.       ;; it: all non-stream devices behave in the same way.  Perhaps
  3630.       ;; the cleanest way is to make the concept of a "redisplayable"
  3631.       ;; device, which stream devices are not.  Look into this more if
  3632.       ;; we ever create another non-redisplayable device type (e.g.
  3633.       ;; processes?  printers?).
  3634.  
  3635.       ;; Don't redisplay the echo area if we are executing a macro.
  3636.       (if (not executing-kbd-macro)
  3637.       (if (eq 'stream (frame-type frame))
  3638.           (send-string-to-terminal message stdout-p)
  3639.         (redisplay-echo-area))))))
  3640.  
  3641. (defun display-message (label message &optional frame stdout-p)
  3642.   "Print a one-line message at the bottom of the frame.  First argument
  3643. LABEL is an identifier for this message.  MESSAGE is the string to display.
  3644. Use `clear-message' to remove a labelled message.
  3645.  
  3646. Here are some standard labels (those marked with `*' are not logged
  3647. by default--see the `log-message-ignore-labels' variable):
  3648.     message       default label used by the `message' function
  3649.     error         default label used for reporting errors
  3650.   * progress      progress indicators like \"Converting... 45%\"
  3651.   * prompt        prompt-like messages like \"I-search: foo\"
  3652.   * no-log        messages that should never be logged"
  3653.   (clear-message label frame stdout-p t)
  3654.   (append-message label message frame stdout-p))
  3655.  
  3656. (defun current-message (&optional frame)
  3657.   "Returns the current message in the echo area, or nil.
  3658. The FRAME argument is currently unused."
  3659.   (cdr (car message-stack)))
  3660.  
  3661. ;;; may eventually be frame-dependent
  3662. (defun current-message-label (&optional frame)
  3663.   (car (car message-stack)))
  3664.  
  3665. (defun message (fmt &rest args)
  3666.   "Print a one-line message at the bottom of the frame.
  3667. The arguments are the same as to `format'.
  3668.  
  3669. If the only argument is nil, clear any existing message; let the
  3670. minibuffer contents show."
  3671.   ;; questionable junk in the C code
  3672.   ;; (if (framep default-minibuffer-frame)
  3673.   ;;     (make-frame-visible default-minibuffer-frame))
  3674.   (if (and (null fmt) (null args))
  3675.       (progn
  3676.     (clear-message nil)
  3677.     nil)
  3678.     (let ((str (apply 'format fmt args)))
  3679.       (display-message 'message str)
  3680.       str)))
  3681.  
  3682. ;;;;;;
  3683. ;;;;;; warning stuff
  3684. ;;;;;;
  3685.  
  3686. (defcustom log-warning-minimum-level 'info
  3687.   "Minimum level of warnings that should be logged.
  3688. The warnings in levels below this are completely ignored, as if they never
  3689. happened.
  3690.  
  3691. The recognized warning levels, in decreasing order of priority, are
  3692. 'emergency, 'alert, 'critical, 'error, 'warning, 'notice, 'info, and
  3693. 'debug.
  3694.  
  3695. See also `display-warning-minimum-level'.
  3696.  
  3697. You can also control which warnings are displayed on a class-by-class
  3698. basis.  See `display-warning-suppressed-classes' and
  3699. `log-warning-suppressed-classes'."
  3700.   :type '(choice (const emergency) (const alert) (const critical)
  3701.          (const error) (const warning) (const notice)
  3702.          (const info) (const debug))
  3703.   :group 'warnings)
  3704.  
  3705. (defcustom display-warning-minimum-level 'info
  3706.   "Minimum level of warnings that should be displayed.
  3707. The warnings in levels below this are completely ignored, as if they never
  3708. happened.
  3709.  
  3710. The recognized warning levels, in decreasing order of priority, are
  3711. 'emergency, 'alert, 'critical, 'error, 'warning, 'notice, 'info, and
  3712. 'debug.
  3713.  
  3714. See also `log-warning-minimum-level'.
  3715.  
  3716. You can also control which warnings are displayed on a class-by-class
  3717. basis.  See `display-warning-suppressed-classes' and
  3718. `log-warning-suppressed-classes'."
  3719.   :type '(choice (const emergency) (const alert) (const critical)
  3720.          (const error) (const warning) (const notice)
  3721.          (const info) (const debug))
  3722.   :group 'warnings)
  3723.  
  3724. (defvar log-warning-suppressed-classes nil
  3725.   "List of classes of warnings that shouldn't be logged or displayed.
  3726. If any of the CLASS symbols associated with a warning is the same as
  3727. any of the symbols listed here, the warning will be completely ignored,
  3728. as it they never happened.
  3729.  
  3730. NOTE: In most circumstances, you should *not* set this variable.
  3731. Set `display-warning-suppressed-classes' instead.  That way the suppressed
  3732. warnings are not displayed but are still unobtrusively logged.
  3733.  
  3734. See also `log-warning-minimum-level' and `display-warning-minimum-level'.")
  3735.  
  3736. (defcustom display-warning-suppressed-classes nil
  3737.   "List of classes of warnings that shouldn't be displayed.
  3738. If any of the CLASS symbols associated with a warning is the same as
  3739. any of the symbols listed here, the warning will not be displayed.
  3740. The warning will still logged in the *Warnings* buffer (unless also
  3741. contained in `log-warning-suppressed-classes'), but the buffer will
  3742. not be automatically popped up.
  3743.  
  3744. See also `log-warning-minimum-level' and `display-warning-minimum-level'."
  3745.   :type '(repeat symbol)
  3746.   :group 'warnings)
  3747.  
  3748. (defvar warning-count 0
  3749.   "Count of the number of warning messages displayed so far.")
  3750.  
  3751. (defconst warning-level-alist '((emergency . 8)
  3752.                 (alert . 7)
  3753.                 (critical . 6)
  3754.                 (error . 5)
  3755.                 (warning . 4)
  3756.                 (notice . 3)
  3757.                 (info . 2)
  3758.                 (debug . 1)))
  3759.  
  3760. (defun warning-level-p (level)
  3761.   "Non-nil if LEVEL specifies a warning level."
  3762.   (and (symbolp level) (assq level warning-level-alist)))
  3763.  
  3764. ;; If you're interested in rewriting this function, be aware that it
  3765. ;; could be called at arbitrary points in a Lisp program (when a
  3766. ;; built-in function wants to issue a warning, it will call out to
  3767. ;; this function the next time some Lisp code is evaluated).  Therefore,
  3768. ;; this function *must* not permanently modify any global variables
  3769. ;; (e.g. the current buffer) except those that specifically apply
  3770. ;; to the warning system.
  3771.  
  3772. (defvar before-init-deferred-warnings nil)
  3773.  
  3774. (defun after-init-display-warnings ()
  3775.   "Display warnings deferred till after the init file is run.
  3776. Warnings that occur before then are deferred so that warning
  3777. suppression in the .emacs file will be honored."
  3778.   (while before-init-deferred-warnings
  3779.     (apply 'display-warning (car before-init-deferred-warnings))
  3780.     (setq before-init-deferred-warnings
  3781.       (cdr before-init-deferred-warnings))))
  3782.  
  3783. #-infodock (add-hook 'after-init-hook 'after-init-display-warnings)
  3784.  
  3785. (defun display-warning (class message &optional level)
  3786.   "Display a warning message.
  3787. CLASS should be a symbol describing what sort of warning this is, such
  3788. as `resource' or `key-mapping'.  A list of such symbols is also
  3789. accepted. (Individual classes can be suppressed; see
  3790. `display-warning-suppressed-classes'.) Optional argument LEVEL can
  3791. be used to specify a priority for the warning, other than default priority
  3792. `warning'. (See `display-warning-minimum-level').  The message is
  3793. inserted into the *Warnings* buffer, which is made visible at appropriate
  3794. times."
  3795.   (or level (setq level 'warning))
  3796.   (or (listp class) (setq class (list class)))
  3797.   (check-argument-type 'warning-level-p level)
  3798.   (if (and (not (featurep 'infodock))
  3799.        (not init-file-loaded))
  3800.       (setq before-init-deferred-warnings
  3801.         (cons (list class message level) before-init-deferred-warnings))
  3802.     (catch 'ignored
  3803.       (let ((display-p t)
  3804.         (level-num (cdr (assq level warning-level-alist))))
  3805.     (if (< level-num (cdr (assq log-warning-minimum-level
  3806.                     warning-level-alist)))
  3807.         (throw 'ignored nil))
  3808.     (if (intersection class log-warning-suppressed-classes)
  3809.         (throw 'ignored nil))
  3810.     
  3811.     (if (< level-num (cdr (assq display-warning-minimum-level
  3812.                     warning-level-alist)))
  3813.         (setq display-p nil))
  3814.     (if (and display-p
  3815.          (intersection class display-warning-suppressed-classes))
  3816.         (setq display-p nil))
  3817.     (save-excursion
  3818.       (let ((buffer (get-buffer-create "*Warnings*")))
  3819.         (when display-p
  3820.           ;; The C code looks at display-warning-tick to determine
  3821.           ;; when it should call `display-warning-buffer'.  Change it
  3822.           ;; to get the C code's attention.
  3823.           (incf display-warning-tick))
  3824.         (set-buffer buffer)
  3825.         (goto-char (point-max))
  3826.         (setq warning-count (1+ warning-count))
  3827.         (princ (format "(%d) (%s/%s) "
  3828.                warning-count
  3829.                (mapconcat 'symbol-name class ",")
  3830.                level) buffer)
  3831.         (princ message buffer)
  3832.         (terpri buffer)
  3833.         (terpri buffer)))))))
  3834.  
  3835. (defun warn (&rest args)
  3836.   "Display a warning message.
  3837. The message is constructed by passing all args to `format'.  The message
  3838. is placed in the *Warnings* buffer, which will be popped up at the next
  3839. redisplay.  The class of the warning is `warning'.  See also
  3840. `display-warning'."
  3841.   (display-warning 'warning (apply 'format args)))
  3842.  
  3843. (defvar warning-marker nil)
  3844.  
  3845. ;; When this function is called by the C code, all non-local exits are
  3846. ;; trapped and C-g is inhibited; therefore, it would be a very, very
  3847. ;; bad idea for this function to get into an infinite loop.
  3848.  
  3849. (defun display-warning-buffer ()
  3850.   "Make the buffer that contains the warnings be visible.
  3851. The C code calls this periodically, right before redisplay."
  3852.   (let ((buffer (get-buffer-create "*Warnings*")))
  3853.     (when (or (not warning-marker)
  3854.           (not (eq (marker-buffer warning-marker) buffer)))
  3855.       (setq warning-marker (make-marker))
  3856.       (set-marker warning-marker 1 buffer))
  3857.     (set-window-start (display-buffer buffer) warning-marker)
  3858.     (set-marker warning-marker (point-max buffer) buffer)))
  3859.  
  3860. (defun emacs-name ()
  3861.   "Return the printable name of this instance of Emacs."
  3862.   (cond ((featurep 'infodock) "InfoDock")
  3863.     ((featurep 'xemacs) "XEmacs")
  3864.     (t "Emacs")))
  3865.  
  3866. ;;; simple.el ends here
  3867.